5

Is there a way to compare two xml nodes in Delphi?

I am using the MSXML parser and loading the xml into an IXmlDOMDocument2. The nodes are stored in an IXmlDOMNode.

I would be ok using Delphi's TXMLDocument if it helps. I do not want to download any third party components.

I just want to compare the structures of the nodes, not the values.

I saw a similar post/utility for Java, but nothing for Delphi.

Thank you!

Community
  • 1
  • 1
sse
  • 987
  • 1
  • 11
  • 30
  • Updated the question, no longer concerned about attributes or values. Just interested in an easy way to compare the two structures. – sse May 10 '13 at 22:19
  • How do you want the differences to be presented/described? – David Heffernan May 10 '13 at 23:09
  • I am pretty flexible on this point. Perhaps three stringlists passed by reference. The first two would identify nodes that exist in one but not the other. The third contains the differences. I know that the nodes drill down, so the strings in the list would contain the node path. – sse May 12 '13 at 01:07

1 Answers1

0

Since you want to compare only the structure, you could convert your nodes into 'full paths' and compare them as strings.

E.g. Let's assume the trees:

Tree A

Root -> Node1 -> Child1
              -> Child2
     -> Node2

Tree B

Root -> Node1 -> Child1
     -> Node2

From the conversion to 'full paths' you'd get:

Tree A

Root
Root\Node1
Root\Node1\Child1
Root\Node1\Child2
Root\Node2

Tree B

Root
Root\Node1
Root\Node1\Child1
Root\Node2

By iterating on the lists of full paths you can quickly check and compare if they exist in the other tree. You can also easily find the nodes on the tree.

mlemanczyk
  • 121
  • 1
  • 8