2つのXMLファイルを比較し、すべての違いを記録しようとしています。この問題は、ノードが繰り返し始めるときに発生します。2つのXMLDocumentツリーを繰り返しノードと比較する
<root>
<a/>
<a/>
<b/>
</root>
と::二つのファイルについて
<root>
<a/>
<b/>
</root>
私のプログラムは、現在どんな違いを記録しません。以下のように(大きくて醜い)メソッドは次のとおりです。
private void searchDocumentTrees (Node nodeA, Node nodeB, ArrayList<String> differences) {
if (nodeA.hasChildNodes() && !nodeB.hasChildNodes()) {
// record A deeper at this node
return;
}
else if (!nodeA.hasChildNodes() && nodeB.hasChildNodes()) {
// record B deeper at this node
return;
}
else if (!nodeA.hasChildNodes() && !nodeB.hasChildNodes()) {
return;
}
NodeList childrenA = nodeA.getChildNodes();
NodeList childrenB = nodeB.getChildNodes();
// indexes of nodes present in both lists of children as
// NodeList doesn't allow searching by value
ArrayList<Integer> presentInBothIndexA = new ArrayList<>();
ArrayList<Integer> presentInBothIndexB = new ArrayList<>();
// check for nodes present in both trees, record those present only in A
for (int indexA = 0; indexA < childrenA.getLength(); indexA++) {
boolean isPresentInBoth = false;
Node currentA = childrenA.item(indexA);
if (currentA.getNodeType() == Node.ELEMENT_NODE) {
for (int indexB = 0; indexB < childrenB.getLength(); indexB++) {
Node currentB = childrenB.item(indexB);
if (currentB.getNodeType() == Node.ELEMENT_NODE) {
// if the nodes match, record their indexes and break from inner loop
if (currentA.getNodeName().equals(currentB.getNodeName())) {
isPresentInBoth = true;
presentInBothIndexA.add(indexA);
presentInBothIndexB.add(indexB);
break;
}
}
}
// if the flag has not been changed currentA is not present in childrenB
if (!isPresentInBoth) {
// record as present only in A
}
}
}
// record nodes present only in B
for (...){
/* same nested loop - this time the outer is iterating over B
and matching nodes indexes are not recorded - record only B - A */
}
for (int indexBoth = 0, len = presentInBothIndexA.size(); indexBoth < len; indexBoth++) {
Node currentA = childrenA.item(presentInBothIndexA.get(indexBoth));
Node currentB = childrenB.item(presentInBothIndexB.get(indexBoth));
searchDocumentTrees(currentA,currentB,differences);
}
}
私の最初の概念は両方のファイルで出現のカウンタのisPresentInBothフラグを置き換えることでしたが、これはおそらく、もっと複雑になるため、第3のループをご紹介します。あなたは良いアイデアを持っていますか?