解析XML与DOM中不同层次具有相同名称的标签 [英] Parsing XML with tags with same name on different levels in DOM
问题描述
我已经在这几天并不能找到一个方法来解析这个XML文件。
我试过XMLPullParser,SAX和DOM现在,但只要我已经取得了一些进展,我意识到,我有两个不同的< CON>
标签。我是新来的Java和Android,尤其是新的XML解析,所以任何帮助将意味着很多。
I've been at this for days and cannot find a way to parse this XML file.
I've tried XMLPullParser, SAX and now DOM, but as soon as I'd made some progress, I realized that I have two different <con>
tags. I'm new to Java and Android, and especially new to XML parsing, so any help would mean a lot.
下面是XML文件的一个片段:
Here's a segment of the xml file:
<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>
正如你所看到的,并非所有的部分有一个孩子&LT; CON&GT;
标签,所以我不认为使用计数器会工作
As you can see, not all sections have a child <con>
tag, so I don't think using a counter would work.
下面是我的最新尝试(没有做任何事情的嵌套&LT; CON&GT;
标签):
Here's my latest attempt (doesn't do anything about the nested <con>
tags):
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)--------------------
...
我想有是这样的:
What I'd like to have is something like this:
---------------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
无图案。
我最终会在父解析所有的数据&LT; CON&GT;
标签,但我能明白这一点,如果我知道如何处理孩子&LT; CON&GT;
标记
Again, the document is much longer, and there is no pattern between whether or not there is an idref
.
I will eventually be parsing all the data within the parent <con>
tags, but I would be able to figure that out if I knew how to deal with the child <con>
tag.
我搜索,发现这个:<一href="http://stackoverflow.com/questions/13245874/how-to-differentiate-parent-and-child-tag-while-dom-parsing">How区分父母和孩子的标签,而DOM解析?虽然没有有用的答案。没有问题,我发现在不同的层次相同名称涉及标签。我知道是有很多类似的问题,并就明白了,如果这被删除。虽然再次,我真的AP preciate的帮助。
I searched and found this: How to differentiate parent and child tag while dom parsing? though there were no helpful answers. No questions I found involved tags with same names on different levels. I know there're a lot of similar questions, and will understand if this gets deleted. Though again, I'd really appreciate the help.
推荐答案
试试这个...
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"));
}
}
}
这篇关于解析XML与DOM中不同层次具有相同名称的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!