Your program should behave as follows
(assuming the executable is named "DOMcat"):
> DOMcat test.xml
Total number of nodes: 15
Number of element nodes: 7
Number of attribute nodes: 3
Number of text nodes: 5
Maximal height: 4
Maximal length of sibling list: 4
Number of distinct element names: 7
Number of distinct attribute names: 3
> DOMcat -p1 test.xml
<book isbn="1-2345-6789-0" year="1994"><title>TCP/IP Illustrated</title><author><last>Stevens</last><first>John</first></author><publisher>Addison-Wesley</publisher><price currency="USD">65.95</price></book>
> DOMcat -p2 test.xml
<book isbn="1-2345-6789-0" year="1994">
<title>
TCP/IP Illustrated
</title>
<author>
<last>
Stevens
</last>
<first>
John
</first>
</author>
<publisher>
Addison-Wesley
</publisher>
<price currency="USD">
65.95
</price>
</book>
> DOMcat -p3 test.xml
<book isbn="1-2345-6789-0" year="1994">
<title>
TCP/IP Illustrated
</title>
<author>
<last>
Stevens
</last>
<first>
John
</first>
</author>
<publisher>
Addison-Wesley
</publisher>
<price currency="USD">
65.95
</price>
</book>
As mentioned above, it is perfectly OK if your program always
prints out as first line "<?xml..." for the p1,p2,p3 options.
This might be easier for you to program.
Thus, the
these test runs are also correct.
Node Types
As mentioned above, when counting the total number of nodes you
should only consider the following six types of node:
- Element
- Attribute
- Text
- CDATA
- Comment
- ProcessingInstruction
Note that CDATA-nodes can be within text nodes as the following two
examples
t1.xml and
t2.xml
show. As example
t3.xml shows,
a CDATA-node can also
replace a text node, that is, if the text
solely consists of a CDATA node, then there is
no
surrounding text node, but ONLY a CDATA node. This means that when
looking for text within the DOM you might have to consider CDATA nodes.
> cat t1.xml
<?xml version="1.0"?><book><![CDATA[Bla<<>>>>>blub]]>b</book>
> java DOMTravel -s t1.xml
Total number of nodes: 3
Number of element nodes: 1
Number of attribute nodes: 0
Number of text nodes: 1
Maximal height: 2
Maximal length of sibling list: 1
Number of distinct element names: 1
Number of distinct attribute names: 0
> cat t2.xml
<?xml version="1.0"?><book>b<![CDATA[Bla<<>>>>>blub]]>b</book>
> java DOMTravel -s t2.xml
Total number of nodes: 4
Number of element nodes: 1
Number of attribute nodes: 0
Number of text nodes: 2
Maximal height: 2
Maximal length of sibling list: 2
Number of distinct element names: 1
Number of distinct attribute names: 0
> cat t3.xml
<?xml version="1.0"?><book><![CDATA[Bla<<>>>>>blub]]></book>
> java DOMTravel -s t3.xml
Total number of nodes: 2
Number of element nodes: 1
Number of attribute nodes: 0
Number of text nodes: 0
Maximal height: 1
Maximal length of sibling list: 0
Number of distinct element names: 1
Number of distinct attribute names: 0
Runs on Larger Files
The following 2 XML files are taken from the "Testbed" zip file
from
this page.
For
this xml file you should
get the following for the "s" option:
Total number of nodes: 291
Number of element nodes: 146
Number of attribute nodes: 29
Number of text nodes: 116
Maximal height: 4
Maximal length of sibling list: 29
Number of distinct element names: 6
Number of distinct attribute names: 1
For the p1, p2, and p3 options you should
get
p1,
p2, and
p3
(download and view these using a text editor).
For
this xml file you should
get the following for the "s" option:
Total number of nodes: 433
Number of element nodes: 244
Number of attribute nodes: 27
Number of text nodes: 162
Maximal height: 6
Maximal length of sibling list: 27
Number of distinct element names: 10
Number of distinct attribute names: 1
For the p1, p2, and p3 options you should
get
p1,
p2, and
p3.
Warning: download the files linked below and
inspect them manually. Otherwise your browser might crash..
For
this xml file you should
get the following for the "s" option:
Total number of nodes: 514800
Number of element nodes: 277072
Number of attribute nodes: 733
Number of text nodes: 236994
Maximal height: 9
Maximal length of sibling list: 733
Number of distinct element names: 24
Number of distinct attribute names: 1