Modify the code of Fig. 9.3 (Tree.java) to replace the JAXP default parser with the Xerces SAX 2.0 parser.

Figure 9.3


```
import java.io.*;
import org.xml.sax.*;
import com.ibm.xml.parsers.*;

public class Tree extends HandlerBase {
private int indent = 0; // indention counter

// returns the spaces needed for indenting
private String spacer( int count )
{
String temp = "";

for ( int i = 0; i < count; i++ )
temp += " ";

return( temp );
}

// method called before parsing
// it provides the document location
public void setDocumentLocator( Locator loc )
{
System.out.println( "URL: " + loc.getSystemId() );
}

// method called at the beginning of a document
public void startDocument() throws SAXException
{
System.out.println( "[ document root ]" );
}

// method called at the end of the document
public void endDocument() throws SAXException
{
System.out.println( "[ document end ]" );
}

// method called at the start tag of an element
public void startElement( String name,
AttributeList attributes ) throws SAXException
{
System.out.println( spacer( indent++ ) +
"+-[ element : " + name + " ]");

if ( attributes != null )

for ( int i = 0; i < attributes.getLength(); i++ )
System.out.println( spacer( indent ) +
"+-[ attribute : " + attributes.getName( i ) +
" ] \"" + attributes.getValue( i ) + "\"" );
}

// method called at the end tag of an element
public void endElement( String name ) throws SAXException
{
indent--;
}

// method called when a processing instruction is found
public void processingInstruction( String target,
String value ) throws SAXException
{
System.out.println( spacer( indent ) +
"+-[ proc-inst : " + target + " ] \"" + value + "\"" );
}

// method called when characters are found
public void characters( char buffer[], int offset,
int length ) throws SAXException
{
if ( length > 0 ) {
String temp = new String( buffer, offset, length );

System.out.println( spacer( indent ) +
"+-[ text ] \"" + temp + "\"" );
}
}
// method called when ignorable whitespace is found
public void ignorableWhitespace( char buffer[],
int offset, int length )
{
if ( length > 0 ) {
System.out.println( spacer( indent ) + "+-[ ignorable ]" );
}
}

// method called on a non-fatal (validation) error
public void error( SAXParseException spe )
throws SAXParseException
{
// treat non-fatal errors as fatal errors
throw spe;
}

// method called on a parsing warning
public void warning( SAXParseException spe )
throws SAXParseException
{
System.err.println( "Warning: " + spe.getMessage() );
}

// main method
public static void main( String args[] )
{
boolean validate = false;

if ( args.length != 1 ) {
System.err.println( "Usage: java Tree " +
"[filename]\n" );
System.err.println( "Options:" );
System.exit( 1 );
}

try {
SAXParser saxParser = new SAXParser();
saxParser.setDocumentHandler( new Tree() );
saxParser.parse( args[ 0 ] );
}
catch ( SAXParseException spe ) {
System.out.println( "Parse Error: " + spe.getMessage() );
}
catch ( SAXException se ) {
se.printStackTrace();
}
catch ( IOException ioe ) {
ioe.printStackTrace();
}
catch ( Exception pce ) {
pce.printStackTrace();
}
System.exit( 0 );
}
}
```

Computer Science & Information Technology

You might also like to view...

A Ball class and a Baseball class has what type of relationship?

A. The Ball is a Baseball. B. The Baseball is a Ball. C. The Baseball uses a Ball. D. The Baseball was a Ball.

Computer Science & Information Technology

________ of response is/are the most critical.

A. Accuracy B. Speed C. Cost D. Both accuracy and speed

Computer Science & Information Technology

Project 2010 offers many built-in filters, available using the Filter arrow in the ________ group on the View tab.

A. Display B. Data C. Variable D. Custom

Computer Science & Information Technology

It is possible to specifically defend against the ______ by using a modified version of the TCP connection handling code.

A. three-way handshake B. UDP flood C. SYN spoofing attack D. flash crowd

Computer Science & Information Technology