This blog caters to ABAPers like me, who had to try their hands on Java mapping, and are new to XML and JAVA Concepts
All information here is available in the DOM API documentations, but this page after page documentation is fearful for Starters and ABAPers like me. I would try some simplifications.
I would take a Simple Example of an XML
Oops, the XML looks good, but what is the Document Object Model
All arrows Denote parent Child relationships.
The Green Nodes are the “BASIC BUILDING BLOCKS”, I would call them the Foundation elements, and the tree is build around them. Technically though they are known as ELEMENT Nodes.
The Blue nodes on the other Hand are “FRUIT” nodes of the tree, technically the TEXT nodes, and doing exactly the same, carrying the texts.
The point to remember is that every node in the XML is surrounded by text nodes. In example above, the ROOT or the PERSONS node has 5 Children nodes, 2 ELEMENT nodes and the rest surrounding text nodes. Though it should be considered that the content of the ELEMENT node (NAME) is carried by the text node. Another Point would be that, the text nodes have no names. All say #text when asked for names.
Now let’s enter the world of APIs and methods. This is an ocean, but I would like to show you the pearls here. Let’s try reading a Document tree.
The questions that would come:
1. I have an XML File, how do I convert it to a tree like above?
Remember just three steps here.
a) Get the Document Builder Factory
b) Get the parser
c) Parse your File using the builder.
Once done, you tree “DOC” is ready.
2. How to Find the ROOT of the tree?
Many ways, but the Simplest is:
This would return the PERSONS node.
Import : import org.w3c.dom.Element;
3. Now I need to traverse through various nodes of the tree?
Many options available, I would show just couple of them .
Let’s first understand the concept of a Nodelist.
To put in simple words it is a set of Nodes say for example ( person, name .etc ).
And now the options:
a) Use the “Child-Sibling” Link
Having a look at the object model explains,
the node Persons has 2 child nodes PERSON.
Each of these nodes is a sibling of each other. Let us
have a look at 2 mostcommon methods to find all
the “Child-Sibling” relationships.
This method gives you the first Child of the ROOT, say PERSON ( with name = Abhishek ).
Usage PERSONS. getFirstChild ()
This method then gives you the Sibling
the next PERSON node ( with name = abhishek1 ).
Usage PERSONS. getFirstChild ().getNextSibling ()
Using both the methods in conjunction
in a recursive manner, gives you the full tree.
Another Option is to get all children of the ROOT in one go.
The usage of the following method getChildNodes() helps.
This would return a NodeList of all child nodes
Usage : NodeList ChildList = PERSONS. getChildNodes()
4. Node Name and Node Value?
Node Name is only available for ELEMENT Node. For a text node it is #text. Node value is null for a ELEMENT node, it is only available for the text node. To get the node name use the method getNodeName ( ); To get the value of an element node use the following Code
Import : import org.w3c.dom.Node;
This would return ABHISHEK
5. How to identify ELEMENT or TEXT nodes?
Use the getNodeType() to know the same. This would return values equal to NODE.ELEMENT_NODE or NODE.TEXT_NODE.
For example PERSON.getNodeType () = NODE.ELEMENT_NODE
Hope this Set of information helps you. Just to indicate a Little Further DOM is not the most performant ways to Read a XML document. The Better alternative is SAX. Though in some cases, DOM need to used