Java XML Programming (sax)

Source: Internet
Author: User
Tags end interface xml parser
Xml| programming the current SAX API has two versions. We use the second edition (see Resources) to do the example. The class and method names in the second edition are not the same as the first version, but the structure of the code is the same.





Sax is a set of APIs, not a parser, so this code is generic in the XML parser. To get the example running, you'll need an XML parser that supports SAX v2. I use Apache's xerces parser. (see Resources) refer to your parser's getting-started documentation to get the data that invokes a SAX parser.





SAX API instructions are easy to read. It contains a lot of detailed content. The primary task of using the Sax API is to create a ContentHandler interface, a callback interface that the XML parser invokes to distribute Sax events that occur when parsing an XML document to a handler.





for convenience, the SAX API also provides a DefaultHandler adapter class that has implemented the ContentHandler interface.





once you have implemented the ContentHandler or extended the DefaultHandler class, you simply parse the XML parser into a specific document.





Our first example expands DefaultHandler to print each SAX event to the console. This will give you a preliminary image of what the Sax event will occur and in what order.





as a start, here's a sample of the XML document that will be used in our first example:








<?xml version= "1.0"?>


<simple date= "7/7/2000" >


<name> Bob </name>


<location> New York </location>


</simple>








Code:


package xmltest;


import javax.xml.parsers.*;


import org.xml.sax.*;


import org.xml.sax.helpers.*;


import java.io.*;














public class Saxtest extends DefaultHandler {







Methods for
//overloading DefaultHandler classes


//To intercept Sax event notifications.


//


//About all valid events, see Org.xml.sax.ContentHandler


//


public void Startdocument () throws Saxexception {


System.out.println ("SAX event:start DOCUMENT");


}





public void Enddocument () throws Saxexception {


System.out.println ("SAX event:end DOCUMENT");


}





public void Startelement (String NamespaceURI,


String LocalName,


String QName,


Attributes attr) throws Saxexception {


System.out.println ("SAX event:start element[" +


localname + "]");





//If there is a property, we also print out ...


for (int i = 0; i < attr.getlength (); i++) {


System.out.println ("ATTRIBUTE:" +


Attr.getlocalname (i) +


"VALUE:" +


Attr.getvalue (i));


}





}





public void EndElement (String NamespaceURI,


String LocalName,


String qName) throws Saxexception {


System.out.println ("SAX event:end element[" +


localname + "]");


}





public void characters (char[] ch, int start, int length)


throws Saxexception {





System.out.print ("SAX event:characters[");





try {


OutputStreamWriter OUTW = new OutputStreamWriter (System.out);


outw.write (CH, start,length);


Outw.flush ();


} catch (Exception e) {


E.printstacktrace ();


}





System.out.println (")");





}








public static void Main (string[] argv) {





System.out.println ("Example1 SAX Events:");


try {





//SAXParserFactory spfactory = Saxparserfactory.newinstance ();


//SAXParser sparser = Spfactory.newsaxparser ();








//Create sax 2 parser ...


XMLReader XR = xmlreaderfactory.createxmlreader ("Org.apache.xerces.parsers.SAXParser");





//Installation ContentHandler ...


Xr.setcontenthandler (New Saxtest ());





//Parsing file ...


Xr.parse (New InputSource (


new FileReader ("Examplea.xml"));








}catch (Exception e) {


E.printstacktrace ();


}





}





}











need to download XML parser Http://xml.apache.org/dist/xerces-j/Xerces-J-bin.2.5.0.zip





jdom parsing xml:





http://chinaunix.net/forum/viewtopic.php?p=865198#865198


recommended to download JDOM8 bag














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.