Two ways of parsing XML, dom4j and SAX parse

Source: Internet
Author: User
Tags xpath

DOM4J and Sax difference.

    • DOM4J parsing is slow and consumes memory because the file is put into memory before parsing. and using object-based model parsing

  the following points are available :

1. Dom4j parse loads the entire XML file into memory before parsing.  2. It uses Object based Model for parsing XML.  3. High memory Usage-as It loads the file to memory.  4. We can insert or delete nodes.  5. Traverse in any direction. 6. With dom4j we can use all the XPATH expressions. The code is as follows:
1Saxreader reader =NewSaxreader ();2InputStream is = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Data.xml");3 //InputStream is = new FileInputStream (New File ("C:/test/data.xml"));4Document doc =Reader.read (IS);5Element ele =doc.getrootelement ();6@SuppressWarnings ("Unchecked")7list<element> list =ele.elements ();8              for(Element elelist:list) {9                 if("Driver". Equals (Elelist.getname ())) {TenDriver =Elelist.gettexttrim (); One                 } A                 if("url". Equals (Elelist.getname ())) { -URL =Elelist.gettexttrim (); -                 } the                 if("Password". Equals (Elelist.getname ())) { -Password =Elelist.gettexttrim (); -                 } -                 if("username". Equals (Elelist.getname ())) { +Username =Elelist.gettexttrim (); -                 } +                      A}

    • SAX parsing is fast, memory consumption is small, and event model parsing is used.

the following points are available :

1. SAX parses the XML file node by node.  2. It uses Event based Model for parsing XML.  3. Low memory usage as it does not loads the entire file to memory instead read it node by node.  4. We cant insert or delete a node.  5. Top to bottom traversing.   6. With SAX parser we can only use the basic XPATH expressions and can not be expressions like last etc.    You can use Sax for reading large file XML, if you want to use a complex XPath expression, you can use DOM4J.  When using sax parsing we are going to inherit DefaultHandler, where there are startdocument (), Enddocument (), startelement (), EndElement (), characters () etc. method. When we parse it, we parse it one line at a time, put it in the characters () method, and then you can take the values of each element in the EndElement () method. The code is as follows:
1  PackageCom.devil.util;2 3 Importjava.io.IOException;4 5 Importjavax.xml.parsers.ParserConfigurationException;6 ImportJavax.xml.parsers.SAXParser;7 Importjavax.xml.parsers.SAXParserFactory;8 9 Importorg.xml.sax.Attributes;Ten Importorg.xml.sax.SAXException; One ImportOrg.xml.sax.helpers.DefaultHandler; A  -  Public classSaxparseextendsDefaultHandler { -     Private StaticString Driver =NULL; the     Private StaticString URL =NULL; -     Private StaticString username =NULL; -     Private StaticString Password =NULL; -     PrivateString Elementvalue =NULL; +  -     Static { +SAXParserFactory factory =saxparserfactory.newinstance (); A         Try { atSAXParser parse =Factory.newsaxparser (); -Parse.parse (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Data.xml"),Newsaxparse ()); -}Catch(parserconfigurationexception e) { - e.printstacktrace (); -}Catch(saxexception e) { - e.printstacktrace (); in}Catch(IOException e) { - e.printstacktrace (); to         } +          -     } the @Override *      Public voidstartelement (String paramString1, String localname, $String QName, Attributes pattr)throwssaxexception {Panax Notoginseng     } -  the @Override +      Public voidCharacters (Char[] ch,intStartintEndthrowssaxexception { AElementvalue =NewString (CH, start, end); the  +     } -  $ @Override $      Public voidendElement (String namespaceurl, String localname, String QName) -             throwssaxexception { -  the         if(Qname.equals ("Driver")) { -Driver =Elementvalue.trim ();Wuyi         } the         if("url". Equals (QName)) { -URL =Elementvalue.trim (); Wu         } -         if("Password". Equals (QName)) { AboutPassword =Elementvalue.trim (); $         } -         if("username". Equals (QName)) { -Username =Elementvalue.trim (); -         } A  +     } the  -      Public StaticString Getdriver () { $         returndriver; the     } the  the      Public StaticString GetUrl () { the         returnURL; -     } in  the      Public StaticString GetUserName () { the         returnusername; About     } the  the      Public StaticString GetPassword () { the         returnpassword; +     } -  the     Bayi      Public Static voidMain (string[] args) { the          the System.out.println (Saxparse.getdriver ()); -     } -}

Two ways of parsing XML, dom4j and SAX parse

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.