Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import jav

ID: 441286 • Letter: I

Question

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

public class SearchXML extends DefaultHandler {
private int indent = 0; // indention counter
public boolean isfound = false;
public static String myday;

public static String mymonth;



// 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 at the beginning of a document
public void startDocument() throws SAXException
{
System.out.println( "<?xml version = "1.0"?>" );
}

// 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 uri, String eleName,
String raw, Attributes attributes ) throws SAXException
{

if(eleName.equals("date"))
if( attributes.getValue(1).equals(myday)){
isfound=true;
System.out.print( spacer( indent ) + "<" + eleName );
if ( attributes != null )
for ( int i = 0; i < attributes.getLength(); i++ )
System.out.print( " "+ attributes.getLocalName( i ) +
" = " + """ +
attributes.getValue( i ) + """ );
System.out.println( ">" );
indent += 3;
}
else
isfound=false;
}

// method called at the end tag of an element
/*public void endElement( String uri, String eleName,
String raw ) throws SAXException
{
indent -= 3;
System.out.println( spacer(indent) + "</" + eleName + ">");
}*/

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

if ( !temp.trim().equals( "" ) )
System.out.println( spacer(indent) + temp.trim() );
}
}

// method called when a processing instruction is found
/* public void processingInstruction( String target,
String value ) throws SAXException
{
System.out.println( spacer( indent ) +
"<?" + target + " " + value + "?>");
}*/

// main method
public static void main( String args[] )
{
myday=args[1];
try {
XMLReader saxParser = ( XMLReader ) Class.forName(
"org.apache.xerces.parsers.SAXParser" ).newInstance();

saxParser.setContentHandler( new SearchXML() );
FileReader reader = new FileReader( args[ 0 ] );
saxParser.parse( new InputSource( reader ) );
}
catch ( SAXParseException spe ) {
System.err.println( "Parse Error: " + spe.getMessage() );
}
catch ( SAXException se ) {
se.printStackTrace();
}
catch ( Exception e ) {
e.printStackTrace();
}

System.exit( 0 );
}
}

Explanation / Answer

whats the question here?