Visualizing XML document elements

exercise No. 15

Q:

Consider an arbitrary XML document. The following is just an example:

<memo>
  <from>
    <name>Martin</name>
    <surname>Goik</surname>
  </from>
  <to>
    <name>Adam</name>
    <surname>Hacker</surname>
  </to>
  <to>
    <name>Eve</name>
    <surname>Intruder</surname>
  </to>
  <date year="2005" month="1" day="6"/>
  <subject>Firewall problems</subject>
  <content>
    <para>Thanks for your excellent work.</para>
    <para>Our firewall is definitely broken!</para>
  </content>
</memo>

Write a Java application which lists all elements by depth first pre order traversal among with their respective nesting depth and attributes.

Provide the total numbers of elements and attributes as well. For the above input the expected output of your application is:

Element memo, nesting depth = 0
Element from, nesting depth = 1
Element name, nesting depth = 2
Element surname, nesting depth = 2
Element to, nesting depth = 1
Element name, nesting depth = 2
Element surname, nesting depth = 2
Element to, nesting depth = 1
Element name, nesting depth = 2
Element surname, nesting depth = 2
Element date, nesting depth = 1, Attributes: year = '2005' month = '1' day = '6'
Element subject, nesting depth = 1
Element content, nesting depth = 1
Element para, nesting depth = 2
Element para, nesting depth = 2

Document contains 15 elements and 3 attributes.

A: