Read contests. xml
<? XML version = "1.0" ?>
<! -- Java and XML Contents -->
< Book >
< Title > Java and XML </ Title >
<! -- Chapter list -->
< Contents >
< Chapter Title = "Introduction" Number = "1" >
< Topic Name = "XML matters" />
< Topic Name = "What's important" />
< Topic Name = "The essentials" />
< Topic Name = "What & apos; s next? " />
</ Chapter >
< Chapter Title = "Nuts and bolts" Number = "2" >
< Topic Name = "The basics" />
< Topic Name = "Constraints" />
< Topic Name = "Transformations" />
< Topic Name = "And more" />
< Topic Name = "What & apos; s next? " />
</ Chapter >
< Chapter Title = "Sax" Number = "3" >
< Topic Name = "Getting prepared" />
< Topic Name = "Sax readers" />
< Topic Name = "Content handlers" />
< Topic Name = "Gotcha! " />
< Topic Name = "What & apos; s next? " />
</ Chapter >
</ Contents >
</ Book >
Java files
PackageCom. test;
Import Org. XML. sax. attributes;
Import Org. XML. sax. inputsource;
Import Org. XML. sax. saxexception;
Import Org. XML. sax. xmlreader;
Import Org. XML. sax. helpers. defaulthandler;
Import Org. XML. sax. helpers. xmlreaderfactory;
Public class saxxml extends defaulthandler {
private int index1 = 0 ;
private int index2 = 0 ;
Public VoidCharacters (Char[] CH,IntStart,IntLength)
ThrowsSaxexception {
Try {
String Str = New String (CH, start, length );
If (Str. Trim (). Length () > 0 ){
System. Out. println ( " This book < " + Str + " > Directory " );
}
}Catch(Exception e ){
E. printstacktrace ();
}
}
Public void enddocument () throws saxexception {
system. out. println ( " resolution ended: " );
}
Public void endelement (string Uri, string localname, string QNAME)
throws saxexception {
If (QNAME. equalsignorecase ( " chapter " ))
This . index2 = 0 ;
}
Public void startdocument () throws saxexception {
system. out. println ( " resolution start: " );
}
Public VoidStartelement (string Uri, string localname, string QNAME,
Attributes ATTS)ThrowsSaxexception {
If (QNAME. inclusignorecase ( " Chapter " )){
Index1 ++ ;
For ( Int I = 0 ; I < ATTS. getlength (); I ++ ){
String attname = ATTS. getqname (I );
If (Attname. inclusignorecase ( " Title " )){
System. Out. println ( " The " + Index1 + " Chapter: " + ATTS. getvalue (I ));
}
}
}
If (QNAME. inclusignorecase ( " Topic " )){
Index2 ++ ;
For ( Int I = 0 ; I < ATTS. getlength (); I ++ ){
String attname = ATTS. getqname (I );
If (Attname. inclusignorecase ( " Name " )){
System. Out. println ( " The " + Index2 + " Part: "
+ ATTS. getvalue (I ));
}
}
}
}
Public Static Void Main (string [] ARGs) Throws Exception {
String xmluri = " Contents. xml " ; // Relative path
Xmlreader = Xmlreaderfactory. createxmlreader ();
Reader. setcontenthandler ( New Saxxml ());
Inputsource = New Inputsource (xmluri );
Reader. parse (inputsource );
/*Method 2
Saxparserfactory factory = saxparserfactory. newinstance ();
Factory. setnamespaceaware (true );
Factory. setvalidating (true );
Saxparser parser = factory. newsaxparser ();
Parser. parse ("contents. xml", new saxxml ());*/
}
}
> Result:
Resolution start:
<Java and XML>
Chapter 2: Introduction
Part 1: XML matters
Part 1: What's important
Part 1: The Essentials
Part 1: What's next?
Chapter 2: nuts and bolts
Part 1: the basics
Part 1: Constraints
Part 1: Transformations
Part 1: And more...
Part 1: What's next?
Chapter 2: Sax
Part 1: getting prepared
Part 1: sax readers
Part 1: Content handlers
Part 1: Gotcha!
Part 1: What's next?
Resolution ended: