如何在 Android 应用程序中解析 XML 文件

发布于 2024-10-27 15:35:45 字数 614 浏览 5 评论 0原文

我正在尝试解析一个 XML 文件,如下所示

<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>

,我可以计算章节数。章节数等于问题和答案的数量。我还在我的布局中放置了一个名为“确定”的按钮。

现在我想显示第一个问题,单击“确定”后我想显示第二个问题,直到结束。当我到达最后一个问题时,我想转向新的活动。

如何执行此操作,请帮助我

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖彼岸 2024-11-03 15:35:45

将 xml 读入 InputStream 然后:

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

然后您可以使用 doc,如下所示:

  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 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++){
     ...
痕至 2024-11-03 15:35:45

将 XML 读入文档( v = xml 字符串)

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;

}

然后获取元素,如下所示:

/** 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 "";
 }

如何使用:

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

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);            
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文