1

我已经在这几天了,找不到解析这个 XML 文件的方法。我已经尝试过 XMLPullParser、SAX 和现在的 DOM,但是一旦我取得了一些进展,我就意识到我有两个不同的<con>标签。我是 Java 和 Android 的新手,尤其是 XML 解析的新手,所以任何帮助都意义重大。

这是 xml 文件的一部分:

<data>
      <con id="f3cVQQjz8jr">
        <con idref="nJ4haotQTo0"/>
        <added order="9">2013-08-22T03:14:13.439Z</added>
        <name>Alex</name>
        <rank>0</rank>
      </con>
      <con id="nJ4haotQTo0">
        <added order="10">2013-08-22T03:14:13.439Z</added>
        <name>Charley</name>
        <rank>-2</rank>
      </con>
      <con id="jadh7bH25mI">
        <added order="11">2013-08-22T03:14:13.439Z</added>
        <name>David</name>
        <rank>1227133510</rank>
      </con>
      <con id="erfhZ_dn0HA">
        <con idref="nJ4haotQTo0"/>
        <added order="12">2013-08-22T03:14:13.439Z</added>
        <name>Sebastien</name>
        <rank>1073741824</rank>
      </con>
</data>

如您所见,并非所有部分都有子<con>标签,所以我认为使用计数器不会起作用。

这是我最近的尝试(不对嵌套<con>标签做任何事情):

 public void parseContext()
    {
        NodeList nodeList = doc.getElementsByTagName("con");
        int size = nodeList.getLength();
        for(int i = 0 ; i < size ; i++)
        {


            System.out.println("---------------Context ("+i+")--------------------");
            Node node = nodeList.item(i);
            if(node.getNodeType() == Node.ELEMENT_NODE)
            {
                Element e = (Element) node;
                NodeList resultNodeList = e.getElementsByTagName("name");
                int resultNodeListSize = resultNodeList.getLength();
                for(int j = 0 ; j < resultNodeListSize ; j++ )
                {
                    Node resultNode = resultNodeList.item(j);
                    if(resultNode.getNodeType() == Node.ELEMENT_NODE)
                    {
                        Element resultE = (Element) resultNode;
                        System.out.println("Context Name :"+resultE.getTextContent());
                    }
                }
            }
        }

    }

结果是:

---------------Context (0)--------------------
Context Name :Alex
---------------Context (1)--------------------
Context Name :Charley
---------------Context (2)--------------------
---------------Context (3)--------------------
Context Name :David
---------------Context (4)--------------------
Context Name :Sebastien
---------------Context (5)--------------------
...

我想要的是这样的:

---------------Context (0)--------------------
Context Name :Alex
Context ID Ref: duRjfksjf0
---------------Context (1)--------------------
Context Name :Charley
Context ID Ref: null
---------------Context (3)--------------------
Context Name :David
Context ID Ref: null
---------------Context (4)--------------------
Context Name :Sebastien
Context ID Ref: iJ4hasftQTo0
---------------Context (5)--------------------
....

同样,文档要长得多,并且在是否存在idref. 我最终将解析父<con>标签中的所有数据,但如果我知道如何处理子标签,我将能够弄清楚这一点<con>

我搜索并发现了这个:如何在dom解析时区分父子标签?尽管没有有用的答案。我发现没有任何问题涉及在不同级别上具有相同名称的标签。我知道有很多类似的问题,如果这被删除,我会理解的。不过,我真的很感谢您的帮助。

4

3 回答 3

0

尝试这个 ...

if(node.getNodeType() == Node.ELEMENT_NODE){
    Element e = (Element) node;
    NodeList resultNodeList = e.getElementsByTagName("con");           

    if(!e.hasAttribute("idref")){
        if(resultNodeList.getLength() == 2){
            Element nameElement = (Element) resultNodeList.item(1); 
            if(!nameElement.hasAttribute("idref"))
                 System.out.println("Context ID Ref : "+null);
            else    
                 System.out.println("Context ID Ref :"+nameElement.getAttribute("idref"));
        }else {
            Element nameElement = (Element) resultNodeList.item(0);   
            if(!nameElement.hasAttribute("idref"))
                 System.out.println("Context ID Ref : "+null);
            else    
                 System.out.println("Context ID Ref :"+nameElement.getAttribute("idref"));
        }
    }
}
于 2013-08-23T08:27:37.463 回答
0

这与我刚刚为我的项目所做的类似。我更喜欢写一些常用的方法,以便在其他地方重用。

package com.practice;

import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class XMLHelper {

public static void main(String[] args) {
    parseContext();
}

public static Document createDocumentFromFile() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document document = null;

    try {
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();

        document = db.parse("xmltest.txt");

    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

public static Element converDocumentToElement(Document document) {
    Element rootElement = null;

    if (document != null) {
        document.getDocumentElement().normalize();
        rootElement = document.getDocumentElement();
    }

    return rootElement;
}


public static List<Element> getElementsByName(Element elem, String name) {
    List<Element> elements = null;

    if (elem == null) return elements;

    NodeList nodeList = elem.getElementsByTagName(name);

    if (nodeList == null || nodeList.getLength() == 0) return elements;

    elements = new ArrayList<Element>();

    for (int i=0; i<nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        elements.add(element);
    }

    return elements;
}


public static void parseContext() {
    Document document = createDocumentFromFile();
    Element rootElement = converDocumentToElement(document);

    List<Element> outsideConElements = getElementsByName(rootElement, "con");

    if (outsideConElements == null) {
        System.out.println("no <con> is found!");
    } else {
        for (Element outsideConElement : outsideConElements) {
            List<Element> insideConElements = getElementsByName(outsideConElement, "con");

            if (insideConElements != null && !insideConElements.isEmpty()) {
                Element insideConElement = insideConElements.get(0);
                if (insideConElement != null)
                    System.out.println(insideConElement.getAttribute("idref"));
            }
        }
    }

}

}
于 2013-08-23T04:45:44.377 回答
0
  Node node = nodeList.item(i);     
    NodeList list = node.getChildNodes(); 
    for (int i = 0; i < list.getLength(); i++) {

               Node childnode = list.item(i);

       if ("con".equals(childnode.getNodeName())) {
        //it have a second con tag
       }
             }

看看这个:http ://www.mkyong.com/java/how-to-modify-xml-file-in-java-dom-parser/

于 2013-08-22T22:05:27.037 回答