开发者

How to parse an XML file in an Android app

开发者 https://www.devze.com 2023-02-21 06:09 出处:网络
I am trying to parse an XML file as below <Subject> <chapter> <Question>abc</Question>

I am trying to parse an XML file as below

<Subject>
    <chapter>
       <Question>abc</Question>
       <answer>avksn</answer>
    </chapter>
    <chapter>
        <Question>def</Question>
       <answer>avksn</answer>
    </chapter>
    <chapter>
         <Question>ccsv</Question>
       开发者_运维百科 <answer>avksn</answer>
    </chapter>
</Subject>

in this i am able to count the number of chapter. the number of chapter is equal to number of question and answer. i have also placed a button named as ok in my layout.

now i want to display the first question and after clicking ok i want to display the second question and it goes till the end. When i reached the last question i want to move to a new activity.

how to perform this, pls help me


Read the xml into a InputStream and then:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse([the InpuTstream]);

Then you can use doc like:

  if(doc.getElementsByTagName("GeometryCollection").getLength()>0){
    org.w3c.dom.Node parent_node = doc.getElementsByTagName("GeometryCollection").item(0);
    NodeList nl = parent_node.getChildNodes();
    for(int i = 0;i<nl.getLength();i++){
     ...


Read the XML into a document ( v = the xml string )

public Document XMLfromString(){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(v));
        doc = db.parse(is); 

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return doc;

}

Then get the element like so:

/** Returns element value
  * @param elem element (it is XML tag)
  * @return Element value otherwise empty String
  */
 public final static String getElementValue( Node elem ) {
     Node kid;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                 if( kid.getNodeType() == Node.TEXT_NODE  ){
                     return kid.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

How to use:

Document doc = x.XMLfromString();
NodeList nodes = doc.getElementsByTagName("result");

    for (int i = 0; i < nodes.getLength(); i++) {                           
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("id", x.getValue(e, "orgid"));
        map.put("bedrijf", x.getValue(e, "naam"));
        map.put("plaats", x.getValue(e, "plaats"));
        mylist.add(map);            
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消