It Programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary. Sax Parsing
1. When parsing an XML document using the DOM, the entire XML document needs to be read, the Doucment object representing the entire DOM tree is architected in memory, and the XML document is then manipulated. In this case, if the XML document is particularly large, it consumes a large amount of memory from the computer and is prone to memory overflow.
2.SAX parsing allows documents to be processed when the document is read, without having to wait until the entire document is loaded before the document is manipulated.
3.SAX Parses an XML file in an event-handling manner, parsing an XML document with SAX, involving two parts: a parser and an event handler:
(1) The parser can be created using the JAXP API, and after creating the SAX parser, you can specify the parser to parse an XML document.
(2) When parsing an XML document in sax mode, the parser will call a method of the event handler whenever it resolves to an XML document, and when the parser invokes the method of the event handler, it passes the XML file content currently parsed to the event handler as a parameter of the method.
(3) The event handler is written by the programmer, and the programmer can easily get the data parsed by the SAX parser through the parameters of the method in the event handler, so that it can decide how to process the data.
Five steps to parse an XML document in sax mode:
(1) Create a sax parsing factory using SAXParserFactory
saxparserfactory SPF = saxparserfactory.newinstance ();
(2) Get parser object through sax parsing factory;
SAXParser sp = Spf.newsaxparser ();
(3) Get an XML reader through the parser object
XMLReader XMLReader = Sp.getxmlreader ();
(4) Set the reader's event handler
Xmlreader.setcontenthandler (New Bookparserhandler ());
(5) Parsing XML files
xmlreader.parse ("Book.xml");
Example : Encapsulates each product in a product.xml document into a product object and puts multiple book objects in a list collection to return
public class Product {
Private String specifications;
Private String Price;
Private String notes;
public void Setspecifications (String specifications) {
This.specifications = specifications;
}
public void Setprice (String price) {
This.price = Price;
}
public void Setnotes (String notes) {
This.notes = notes;
}
@Override
Public String toString () {
TODO auto-generated Method Stub
Return "specifications=" +this.specifications+ "\tprice=" +this.price+ "\tnotes=" +this.notes;
}
}
public class Sax_xml_3 {
/**
* Encapsulate each product in the Product.xml document into a Product object,
* and put multiple book objects in a list collection to return
**/
public static void Main (string[] args) throws Parserconfigurationexception, Saxexception, IOException {
1. Create a resolution factory
SAXParserFactory factory = Saxparserfactory.newinstance ();
2. Get the parser
SAXParser sp = Factory.newsaxparser ();
3. Get the reader
XMLReader reader = Sp.getxmlreader ();
4. Setting up the Content processor
Producthandler handler = new Producthandler ();
Reader.setcontenthandler (handler);
5. Reading XML document content
Reader.parse ("Webroot/product.xml");
list<product> list = Handler.getlist ();
for (Product product:list) {
SYSTEM.OUT.PRINTLN (product);
}
}
}
Class Producthandler extends DefaultHandler {
Private String Currenttag;
Private list List = new ArrayList ();
Private product Product = NULL;
@Override
public void Startelement (string uri, String localname, String Name,attributes Attributes) throws Saxexception {
Currenttag = name;
if ("Product". Equals (Currenttag)) {
Product = new product ();
}
}
@Override
public void characters (char[] ch, int start, int length) throws Saxexception {
if ("Specifications". Equals (Currenttag)) {
Product.setspecifications (New String (ch,start,length));
}
if ("Price". Equals (Currenttag)) {
Product.setprice (New String (ch,start,length));
}
if ("Notes". Equals (Currenttag)) {
Product.setnotes (New String (ch,start,length));
}
}
@Override
public void EndElement (string uri, String localname, String name) throws Saxexception {
if (Name.equals ("Product")) {
List.add (product);
Product = NULL;
}
Currenttag = null;//must be empty here;
}
Public List getList () {
return list;
}
}
product.xml file Contents:
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE Catalog SYSTEM "PRODUCT.DTD" >
<catalog id= "Cata1" >
<product category= "Handtool" >
<specifications weight= "2.0kg" > Wrench </specifications>
<price street= "Hong Kong Street" >80.0</price>
<notes> This is the wrench </notes>
</product>
<product category= "Table" >
<specifications>&table;</specifications>
<price street= "&street;" wholesale= "section" >&price;</price>
</product>
</catalog>
Execution output:
specifications= Wrench price=80.0 notes= This is a wrench.
specifications= Desk price=100.0 Notes=null