Xerces2 Sample Program dom.Counter

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:

herong> java_xerces 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:

herong> type hello.xml
<?xml version="1.0"?>
<p>Hello world!</p>

herong> java_xerces dom.Counter hello.xml

hello.xml: 37;7;0 ms (1 elems, 0 attrs, 0 spaces, 12 chars)

Good. The program works. Counts are provided in the output:

The second test is to turn on the validation option "-v | -V Turn on/off validation.":

herong> java_xerces 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.":

herong> java_xerces 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.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

Xerces2 Java Parser - Java API of XML Parsers

 Installing Xerces2 Java Parser for XSD 1.1

 Verify Installation of Xerces2

 Xerces2 Sample Program List

Xerces2 Sample Program dom.Counter

 dom.Counter Validating XML with Associated XSD

 dom.GetElementsByTagName and dom.Writer

 sax.DocumentTracer and sax.Writer

 Examples of XSD and XML Files with Errors

 sax.Writer Reporting Errors Embedded in XML Structure

 XSD 1.1 not Supported by sax.Writer

 XSD 1.1 Supported by jaxp.SourceValidator

 Examples of XSD 1.1 and XML Files with Errors

 jaxp.TypeInfoWriter as an XSD 1.1 Validation Tool

 Using Xerces2 Java APIs

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB