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

One way to drop database objects in a script without throwing errors and cancelling the script if the objects don't exist is to

A. use a PL/SQL block to drop the objects B. use a PL/SQL block to drop the objects and use an IF statement to handle any errors that are thrown C. use a PL/SQL block to drop the objects and ignore any exceptions D. use a PL/SQL block to drop the objects and use a CASE structure to handle any errors that are thrown

Computer Science & Information Technology

A ____ object can be used to create the "scrolling credits" that appear at the end of a film.

A. TextModel B. BackDrop C. BillBoard D. TextAnimation

Computer Science & Information Technology