sax resolves to get all the content of the XML document and get the value of the specified label __javaweb

Source: Internet
Author: User
Tags stub
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;
	
	 }

	
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.