我有一个这样组织的 XML 文件,每个节点下的项目总是按字母顺序排列:
<xml>
<node id="2">
<jack>Jack wrote this.</jack>
<john>John wrote this.</john>
</node>
<node id="4">
<jack>Jack wrote this.</jack>
<jill>Jill wrote this.</jill>
</node>
<node id="9">
<jack>Jack wrote this.</jack>
<james>James wrote this.</james>
<jill>Jill wrote this.</jill>
<john>John wrote this.</john>
</node>
</xml>
如您所见,并非所有名称都在每个节点下。例如,在 中<node id="4">
,John 和 James 没有写任何东西。对于上面的示例,我希望我的程序返回如下内容:
James did not write 2, 4
Jill did not write 2
John did not write 4
我需要跟踪谁没有写什么。我目前正在解析这样的文档:
private static String getTagValue(final Element element)
{
String theId="";
if (element.getTagName().startsWith("node")){
theId = element.getAttribute("id");
return theId;
}
return theId;
}
private static void readXML(String fileName){
for (int index = 0; index < nodeList.getLength(); index++){
Node node = nodeList.item(index);
Element element = (Element) node;
if (node.getNodeType() == Node.ELEMENT_NODE){
// This prints the node id
if(getTagValue(element)!=""){
System.out.println(getTagValue(element)+" = I am the node id number!");
}
// This prints the name
else{
System.out.println(element.getTagName()+" = I am the name!");
}
}
}
}
我想做的是以某种方式将每个节点下的元素与包含所有名称的“控制”列表进行比较,如果它不包含名称,它会返回名称及其父节点。
实际上,我处理的 XML 要大得多,因此性能很重要,但概念是相同的。任何帮助都会很棒。