Kommentar til SAX og DOM parser!!
Hej alle,er der nogen der kunne tilføje par kommentar til de to følgende klasser,så jeg bedre kan forstå hvad der sker.Det toklasser er en SAX og DOM parser,som bruges til at parse en xml fil:
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Parser1 extends DefaultHandler
{
public static void main( String args[] )
{
try {
SAXParser p = SAXParserFactory.newInstance().newSAXParser();
p.parse( new File( "addresses1.xml" ), new Parser1() );
} catch( Exception ex ) {
ex.printStackTrace();
}
}
private int margin;
public void startDocument() throws SAXException
{
System.out.println( "START DOCUMENT" );
margin = 0;
}
public void endDocument() throws SAXException
{
System.out.println( "END DOCUMENT" );
}
public void startElement( String uri, String localName, String qName,
Attributes attributes ) throws SAXException
{
indent( margin );
System.out.print( "START ELEMENT " + qName );
for( int i = 0; i < attributes.getLength(); ++i )
System.out.print( " " + attributes.getQName(i) + "=" + attributes.getValue(i) );
System.out.println();
margin += 3;
}
public void endElement( String uri, String localName, String qName ) throws SAXException
{
margin -= 3;
indent( margin );
System.out.println( "END ELEMENT " + qName );
}
public void characters( char ch[], int start, int length ) throws SAXException
{
String s = new String( ch, start, length );
indent( margin );
System.out.println( "CHARACTERS[" + s + "]" );
}
private void indent( int margin )
{
for( int i = 0; i < margin; ++i )
System.out.print( ' ' );
}
}
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class Parser4
{
public static void main( String args[] )
{
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( new File( "addresses1.xml" ) );
dumpTree( document, 0 );
} catch( Exception ex ) {
ex.printStackTrace();
}
}
private static void dumpTree( Node n, int margin )
{
if( n != null ) {
dumpNode( n, margin );
dumpTree( n.getFirstChild(), margin + 3 );
dumpTree( n.getNextSibling(), margin );
}
}
private static void dumpNode( Node n, int margin )
{
indent( margin );
if( n instanceof Element )
dumpElement( (Element) n );
else if( n instanceof Document )
dumpDocument( (Document) n );
else if( n instanceof Text )
dumpText( (Text) n );
else
System.out.println( n.getNodeName() + " " + n.getNodeType() );
}
private static void dumpElement( Element e )
{
System.out.print( "ELEMENT " + e.getTagName() );
NamedNodeMap atts = e.getAttributes();
if( atts != null )
for( int i = 0; i < atts.getLength(); ++i ) {
Attr a = (Attr) atts.item(i);
System.out.print( " " + a.getName() + "=" + a.getValue() );
}
System.out.println();
}
private static void dumpDocument( Document d )
{
System.out.println( "DOCUMENT" );
}
private static void dumpText( Text t )
{
System.out.println( "TEXT [" + t.getData() + "]" );
}
private static void indent( int margin )
{
for( int i = 0; i < margin; ++i )
System.out.print( ' ' );
}
}
