How to parse an XML file in an Android app

久未见 提交于 2019-12-11 23:37:58

问题


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


回答1:


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++){
     ...



回答2:


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);            
    }


来源:https://stackoverflow.com/questions/5483869/how-to-parse-an-xml-file-in-an-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!