This section provides a tutorial example on how to use dom.Counter to count total number of elements, attributes, etc. in an XML document using DOM technology.
First let's play with the sample program dom.Counter that counts
total number of elements, attributes, etc. using DOM technology.
Here is how to run dom.Counter to get its usage notes:
>jdk8x2r dom.Counter -h
usage: java dom.Counter (options) uri ...
options:
-p name Select parser by name.
-x number Select number of repetitions.
-n |-N Turn on/off namespace processing.
-v |-V Turn on/off validation.
-s |-S Turn on/off Schema validation support.
NOTE: Not supported by all parsers.
-f |-F Turn on/off Schema full checking.
NOTE: Requires use of -s and not supported by all parsers.
-hs|-HS Turn on/off honouring of all schema locations.
NOTE: Requires use of -s and not supported by all parsers.
-va|-VA Turn on/off validation of schema annotations.
NOTE: Requires use of -s and not supported by all parsers.
-dv|-DV Turn on/off dynamic validation.
NOTE: Not supported by all parsers.
-xi|-XI Turn on/off XInclude processing.
NOTE: Not supported by all parsers.
-xb|-XB Turn on/off base URI fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-xl|-XL Turn on/off language fixup during XInclude processing.
NOTE: Requires use of -xi and not supported by all parsers.
-h This help screen.
defaults:
Parser: dom.wrappers.Xerces
Repetition: 1
Namespaces: on
Validation: off
Schema: off
Schema full checking: off
Honour all schema locations: off
Validate annotations: off
Dynamic: off
XInclude: off
XInclude base URI fixup: on
XInclude language fixup: on
The first test is to run dom.Counter on hello.xml with default options:
Good. The program works. Counts are provided in the output:
There is 1 element parsed from the XML file.
There is 0 attribute parsed from the XML file.
There is 0 space parsed from the XML file.
There are 12 characters as element content text parsed from the XML file.
The first test is to turn on the validation option "-v | -V Turn on/off validation.":
>jdk8x2r dom.Counter -v hello.xml
[Error] hello.xml:2:3: Document is invalid: no grammar found.
[Error] hello.xml:2:3: Document root element "p", must match DOCTYPE
^^^ root "null".
hello.xml: 58;7;0 ms (1 elems, 0 attrs, 0 spaces, 12 chars)
Nice. The output confirms that "-v" turns on the DTD validation.
I get a validation error on element "p", because there is no DTD statement defining it.
The third test is to turn on the schema validation option "-s | -S Turn on/off Schema validation support. NOTE: Not supported by all parsers.":
>jdk8x2r dom.Counter -s hello.xml
[Error] hello.xml:2:4: cvc-elt.1.a: Cannot find the declaration of
^^^ element 'p'.
hello.xml: 332;5;0 ms (1 elems, 0 attrs, 0 spaces, 12 chars)
Great. The output confirms that "-s" turns on the XSD (XML Schema) validation.
I get a validation error on element "p", because there is no XSD statement defining it.
See the next section for more tests on XSD validation with dom.Counter.