The XML exercise consists in using two different parsers for XML: StAX and DOM. You need to create two different programs and use them to display an arbitrary XML file using the format explained below.
For example, given an XML file with the following structure:
<root>
<node1>Text1</node1>
<node2>Text2</node2>
<node3>
<node4>
<node5>Text4</node5>
</node4>
</node3>
<node6>
<node7>Text5</node7>
<node8>Test6</node8>
</node6>
</root>
You need to create two different programs, one using DOM and the other using StAX so that they display the example XML structure above, to standard output by using the following output format:
Node: root | Node: node1 | | Data: Text1 | Node: node2 | | Data: Text2 | Node: node3 | | Node: node4 | | | Node: node5 | | | | Data: Text4 | Node: node6 | | Node: node7 | | | Data: Text5 | | Node: node8 | | | Data: Test6
Please note that your programs should work for any XML file provided and not just the example given above. The output format above displays the content of the XML file by imitating a tree structure allowing us to trace the children and the parents of all the nodes in the tree.
Sample implementation ideas
In order to do this exercise, it is highly suggested that you use recursion to print out the nodes. Here are a few brief hints on how to do that:
- For starters, make sure you have a sample XML file you can experiment upon which isn't very complex. Then, pick either DOM or StAX and go through the procedure of reading your XML file using the appropriate classes. You will notice that if the XML file is not properly formatted, the parser will already throw an exception.
- After you've read the XML file using one of the parsers (DOM or StAX), pass the result to a method which will format the output like the exercise requires.