Friday, April 24, 2009

XML as objects in OOPs

I always wondered how can we represent XML tags using code. And, the answer what I came out with was pretty easy and neat.

Each XML begins with a root node and has some nested tags embedded in them. Those tags has more nested tags embedded in between them. Thus they follow a tree like structure.
Also, every node has some attributes associated with it. They are nothing but key value properties of that node.
Consider an XML data shown above

Thus we can represent, each node say root1 as
XML root1 = new XML("root1")

same can be done for sub-root and root2. But, as sub-root are embedded in root2. We can add another line

Now, finally root1 and root2 can be added as children to root.
Thus completing the whole tree structure.

Refer to this page for code and detaills.

Kapil Dalwani

1 comment: