Get all content of XML document package cn.itcast.sax;
Import java.io.IOException;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import Org.xml.sax.ContentHandler;
Import Org.xml.sax.Locator;
Import org.xml.sax.SAXException;
Import Org.xml.sax.XMLReader; public class Test1 {/** * Sax parse XML document * @throws saxexception * @throws parserconfigurationexception * @throws
IOException */public static void main (string[] args) throws Parserconfigurationexception, Saxexception, IOException {
1. Create analytical factory SAXParserFactory factory = Saxparserfactory.newinstance ();
2. Get the parser SAXParser SP = Factory.newsaxparser ();
3. Get reader XMLReader reader = Sp.getxmlreader ();
4. Set Content processor Reader.setcontenthandler (new Listhandler ());
5. Read XML document Content Reader.parse ("Src/book.xml"); }//Get all content of XML document class Listhandler implements contenthandler{@Override public voID startelement (String uri, String localname, String qName, Attributes atts) throws Saxexception {System.out.println
("<" + QName + ">");
for (int i = 0; Atts!= null && i < atts.getlength (); i++) {String AttName = atts.getqname (i);
String Attvalue = Atts.getvalue (i);
System.out.println (attname + "=" + Attvalue); } @Override public void characters (char[] ch, int start, int length) throws Saxexception {System.out.print
ln (new String (ch,start,length)); @Override public void EndElement (string uri, String localname, String qName) throws Saxexception {SYSTEM.OUT.PR
Intln ("</" + QName + ">");
@Override public void Setdocumentlocator (Locator Locator) {//TODO auto-generated method stub} @Override public void Startdocument () throws Saxexception {//TODO auto-generated a stub} @Override public void endd
Ocument () throws Saxexception {//TODO auto-generated method stub} @Overridepublic void startprefixmapping (string prefix, string uri) throws Saxexception {//TODO auto-generated method stub
@Override public void endprefixmapping (String prefix) throws saxexception {//TODO auto-generated method stub @Override public void Ignorablewhitespace (char[] ch, int start, int length) throws Saxexception {//TODO Auto -generated method Stub} @Override the public void ProcessingInstruction (string target, String data) throws Saxexcep tion {//TODO auto-generated method stub} @Override public void skippedentity (String name) throws Saxexception
{//TODO auto-generated method stub}}
Gets the value of the specified label package cn.itcast.sax;
Import java.io.IOException;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import Org.xml.sax.ContentHandler;
Import Org.xml.sax.Locator;
Import org.xml.sax.SAXException; Import org.xMl.sax.XMLReader;
Import Org.xml.sax.helpers.DefaultHandler; public class Text2 {/** * Sax parse XML document * @throws saxexception * @throws parserconfigurationexception * @throws
IOException */public static void main (string[] args) throws Parserconfigurationexception, Saxexception, IOException {
1. Create analytical factory SAXParserFactory factory = Saxparserfactory.newinstance ();
2. Get the parser SAXParser SP = Factory.newsaxparser ();
3. Get reader XMLReader reader = Sp.getxmlreader ();
4. Set Content processor Reader.setcontenthandler (new Tagvaluehandler ());
5. Read XML document Content Reader.parse ("Src/book.xml"); }//Get the value of the specified label class Tagvaluehandler extends defaulthandler{private String currenttag;//Remember what is currently being parsed the label private int NE Ednumber = 2;//Remember to get the value of the first few author tags private int currentnumber;//currently resolves the first few @Override public void startelement (String uri, S
Tring LocalName, String qName, Attributes Attributes) throws saxexception {Currenttag = QName; if (Currenttag.equals ("author")) {Currentnumber++; @Override public void characters (char[] ch, int start, int length) throws Saxexception {if ("Author". Equals (Curre
Nttag) && Currentnumber = = Neednumber) {System.out.println (new String (ch,start,length)); @Override public void EndElement (string uri, String localname, String qName) throws Saxexception {Curren
Ttag = null;
}
}