このように整理された 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">
、ジョンとジェームズは何も書いていません。上記の例では、プログラムが次のようなものを返すようにします。
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 ははるかに大きいため、パフォーマンスが重要になりますが、概念は同じです。どんな助けでも素晴らしいでしょう。