Dom and sax are the differences between manipulating an XML document in an application

Source: Internet
Author: User
Tags event listener

Dom and Sax are the two main APIs for manipulating XML documents in your application, which are explained as follows: The DOM, the Document object model, is called the documents-to-objects module in Chinese. The DOM is a standard document object model defined by the consortium and is a model that is independent of the operating system and programming language and is used for memory storage and manipulation of hierarchical documents. When parsing an XML document by the DOM model, a corresponding DOM tree is constructed in memory that can be used for traversal between different nodes. However, you must first complete the construction of the DOM tree before traversing. Therefore, the processing of large-scale XML documents is very memory-intensive, consuming more resources.        In particular, it is inefficient to manipulate only a small part of the document. SAX, the simple API for XML abbreviation, Chinese is called the XML Easy Application interface. It is a de facto standard. Unlike the DOM, it is an event-driven model. When parsing an XML document, each time you encounter a start or end tag, or a property, or an instruction, the program generates an event to handle accordingly. Therefore, you do not need to parse the entire document before manipulating the document. In fact, parts of a document can be manipulated while parsing. As a result, sax is more suitable for manipulating large documents relative to the DOM. There are some notable differences between sax and DOM, including:
Dom is the first choice for complex object processing, such as when XML is more complex, or when you need to manipulate data in a document at random. Sax moves from the beginning of the document through each node to locate a specific node. The DOM establishes a type description for the document node that is loaded into memory. Ultimately, these descriptions present a potentially large, tree-shaped structure that can be easily moved sideways. If the XML is lengthy, the DOM will show uncontrollable bloat. For example, a 300KB XML document can result in RAM or a 3,000,000kb DOM tree structure in virtual memory. By comparison, it is found that a sax document is not deconstructed at all, and it is not hidden in memory space (of course, some documents are temporarily hidden in memory when the XML stream is read). Sax is a "lighter" technology-it can bring a lighter burden on your system. Sax is the equivalent of watching a marathon, and Dom is like inviting all the contestants to dinner at home. So, how do you choose Sax and Dom? If you are dealing with complex things, such as advanced XSLT transformations, or XPath filtering, choose to use the DOM. If you create or change an XML document, you can also choose the DOM. Instead, you can use Sax to query or read XML documents. Sax can quickly scan a large XML document, and when it finds a query standard, it stops immediately before it is processed. In some cases, the best option in a scenario is to use the DOM and sax to process different parts. For example, you can use the DOM to load XML into memory and change it, and then transfer the final result by sending a sax stream from the DOM tree.
Sax Concepts
Sax is the simple API for XML abbreviation, it is not by the official standards, it can be said that the "folk" the fact of the standard. In fact, it is a community-based discussion product. However, the application of Sax in XML is no less than DOM, and almost all XML parsers will support it.
Sax is a lightweight approach compared to DOM. We know that when we work with the DOM, we need to read through the entire XML document and then create the DOM tree in memory, generating each node object on the DOM tree. This is not a problem when the document is small, but once the document is large, processing the DOM becomes quite time-consuming and laborious. In particular, the need for memory will grow exponentially, so that using the DOM in some applications is not a cost-effective thing (such as in applets). At this point, a good alternative to the solution is sax.
Sax is conceptually completely different from DOM. First, unlike Dom's document-driven, it is event-driven, that is, it does not need to read the entire document, and the document's read-in process is the SAX parsing process. The so-called event-driven, refers to a method based on the callback (callback) mechanism of program operation. (This mechanism is easy to understand if you are clear about the new agent event model for Java)
The XmlReader accepts an XML document and parses it as it is read into the XML document, meaning that the process of reading the document and parsing is done at the same time, which differs greatly from the DOM. Before parsing begins, you need to register a contenthandler with XmlReader, which is the equivalent of an event listener, which defines a number of methods in ContentHandler, such as Startdocument (), which is customized when the parsing process The things that should be handled when the document starts. When XmlReader reads the appropriate content, it throws the corresponding event and proxies the event's processing rights to ContentHandler, calling its corresponding method to respond.
Dom parsing XmlImport java.io.fileinputstream;import Java.io.filenotfoundexception;import java.io.IOException;import Java.io.inputstream;import Javax.xml.parsers.documentbuilder;import javax.xml.parsers.DocumentBuilderFactory; Import Javax.xml.parsers.parserconfigurationexception;import Org.w3c.dom.document;import org.w3c.dom.Element; Import Org.w3c.dom.node;import Org.w3c.dom.nodelist;import Org.xml.sax.saxexception;public class DOMParsePage {    Public Domparsepage () {documentbuilderfactory domfac=documentbuilderfactory.newinstance ();                try {documentbuilder dombuilder=domfac.newdocumentbuilder ();                InputStream is=new FileInputStream ("C:/123.xml");                Document Doc=dombuilder.parse (IS);                Element root=doc.getdocumentelement ();           NodeList books=root.getchildnodes ();                       if (Books!=null) {for (int i=0;i<books.getlength (); i++) {Node book=books.item (i); For (Node node=book.getfirstchild (); node!=null;node=node.getnextsibling ()) { if (Node.getnodetype () ==node.element_node) {if (Node.getnodename (). Equals ("title"                                            ) {String bookname=node.getfirstchild (). Getnodevalue ();                                     System.out.println (BookName); } if (Node.getnodename (). Equals ("author")) {S                                            Tring Author1=node.getfirstchild (). Getnodevalue ();                                    System.out.println (Author1);                                          } if (Node.getnodename (). Equals ("description")) {                                          String addtime=node.getfirstchild (). Getnodevalue ();                        System.out.println (Addtime);           } if (Node.getnodename (). Equals ("PubDate")) {                                          String price=node.getfirstchild (). Getnodevalue ();                                  SYSTEM.OUT.PRINTLN (price); }}}}}}catch (parserconfigurationexception     e) {e.printstacktrace ();      }catch (FileNotFoundException e) {e.printstacktrace ();     }catch (saxexception e) {e.printstacktrace ();     }catch (IOException e) {e.printstacktrace ();     }}public static void Main (string[] args) {new domparsepage ();  }}
Sax parsing xmlpackage SimpleTest; Import org.xml.sax.Attributes; Import org.xml.sax.SAXException; Import Org.xml.sax.Locator; Import Org.xml.sax.ContentHandler; Import Org.xml.sax.InputSource; Import Org.xml.sax.helpers.DefaultHandler; Import Java.io.ioexception;import Javax.xml.parsers.SAXParser;            Import Javax.xml.parsers.saxparserfactory;class Testsax extends DefaultHandler {private StringBuffer buf;           Public Testsax () {super (); } public void Setdocumentlocator (Locator Locator) {} public void startdocument ()                Throws Saxexception {buf=new stringbuffer ();           System.out.println ("******* begins parsing document *******"); } public void Enddocument () throws Saxexception {System.out.println ("******* document parsing ends * * * * *            **"); The public void startprefixmapping (string prefix, string uri) {System.out.println ("\ n prefix mapping:" + prefix + "Start!")           + "Its URI is:" + URI); } public void endprefixmapping (String prefix) {System.out.println ("\ n prefix mapping:"           +prefix+ "End!");} public void ProcessingInstruction (string target, string instruction) throws Saxexception {} publi           c void Ignorablewhitespace (char[] chars, int start, int length) throws Saxexception {} public void Skippedentity (String name) throws saxexception {} public void startelement (stri ng namespaceuri,string localname,string qname,attributes atts) {System.out.println ("******* begins the solution                  Analysis element ******* ");                  System.out.println ("element name" +qname); for (int i=0;i<atts.getlength (); i++) {System.out.println ("element name" +atts.getlocaln                   Ame (i) + "attribute value" +atts.getvalue (i)); }} public VoiD endElement (String namespaceuri,string localname,string fullName) throws Saxexception {System.           OUT.PRINTLN ("****** element parsing end ********");                  } public void characters (char[] chars, int start, int length) throws Saxexception {            accumulate element content into StringBuffer buf.append (chars,start,length); } public static void Main (String args[]) {try{SAXParser                           Factory SF = Saxparserfactory.newinstance ();                           SAXParser sp = Sf.newsaxparser ();                            Testsax testsax=new testsax ();               Sp.parse (New InputSource ("D:\\test\\simpletest\\classes\\simpletest\\test.xml"), Testsax);                      }catch (IOException e) {e.printstacktrace ();                      }catch (saxexception e) {e.printstacktrace ();  }catch (Exception e) {                          E.printstacktrace ();  }          }}

The XML file is as follows: <?xml version= "1.0" encoding= "gb2312"?> <row> <person> <name> Wang Xiaoming </name> < College> School of information </college> <telephone>6258113</telephone> <notes> Male, Ph. D., 1955, PhD, 95 transfer to Hainan University </notes>



Dom and sax are the differences between manipulating an XML document in an application

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.