apache > xerces > xerces-p
XML::Xerces
 

XML::Xerces Sample: SAX2Count

Sample: SAX2Count

SAX2Count

SAX2Count is the simplest application that counts the elements and characters of a given XML file using the (event based) SAX2 API.

Running SAX2Count

The SAX2Count sample parses an XML file and prints out a count of the number of elements in the file. To run SAX2Count, enter the following

perl SAX2Count.pl <XML File>

The following parameters may be set from the command line

Usage:
    perl SAX2Count.pl [options] <XML file | List file>

This program invokes the SAX2XMLReader, and then prints the
number of elements, attributes, spaces and characters found
in each XML file, using SAX2 API.

Options:
    -l          Indicate the input file is a List File that has a list of xml files.
                Default to off (Input file is an XML file).
    -v=xxx      Validation scheme [always | never | auto*].
    -n          Enable namespace processing. Defaults to off.
    -s          Ensable schema processing. Defaults to off.

    -h          Show this help.

  * = Default if not provided explicitly.

-v=always will force validation
-v=never will not use any validation
-v=auto will validate if a DOCTYPE declaration or a schema declaration is present in the XML document

Here is a sample output from SAX2Count

cd samples
perl SAX2Count.pl -v=always personal.xml
personal.xml: 60 ms (37 elems, 12 attrs, 134 spaces, 134 chars)

Running SAX2Count with the validating parser gives a different result because ignorable white-space is counted separately from regular characters.

perl SAX2Count.pl -v=never personal.xml
personal.xml: 10 ms (37 elems, 12 attrs, 0 spaces, 268 chars)

Note that the sum of spaces and characters in both versions is the same.

Note
The time reported by the program may be different depending on your machine processor.