Parsing XML with JAXP and dom4j

Source: Internet
Author: User
Tags xml reader

Objective
原创文章欢迎转载,请保留出处。若有任何疑问建议,欢迎回复。邮箱:[email protected]

There are two main ideas of parsing XML, one is sax (simple API for XML), the other is DOM (Document Object model), in which the JAXP is provided in Java EE to achieve these two kinds of parsing, this paper mainly uses JAXP to implement SAX parsing, At the same time, introduce the popular dom4j to implement DOM parsing.

Sax parsing and Dom parsing comparison
    • Sax parsing
      • Advantages: High efficiency, when encountering a large XML file, do not need to scan the file to be able to operate.
      • Cons: Cannot perform Rud operation, each operation must be re-parsed
    • Dom parsing
      • Pros: Only need to parse XML once
      • Cons: The parsing is complete before it can be manipulated, and memory is consumed when large XML files are encountered.
JAXP Implementing SAX parsing

The first thing to do is to have an XML file

<?xml version="1.0" encoding="UTF-8"?><测试><作者>maxwell_nc</作者><博客>http://blog.csdn.net/maxwell_nc</博客></测试>

Refer to a classic diagram to illustrate sax parsing

Next write the code:

 PackageMaxwellImportJavax.xml.parsers.SAXParser;ImportJavax.xml.parsers.SAXParserFactory;ImportOrg.xml.sax.Attributes;ImportOrg.xml.sax.SAXException;ImportOrg.xml.sax.XMLReader;ImportOrg.xml.sax.helpers.DefaultHandler; Public  class saxpaserdemo {     Public Static void Main(string[] args)throwsException {//Get SAX parser factorySAXParserFactory factory = Saxparserfactory.newinstance ();//Get SAX parser through factorySAXParser parser = Factory.newsaxparser ();//Get XML ReaderXMLReader reader = Parser.getxmlreader ();//Correlation Event ResolverReader.setcontenthandler (NewMycontenthandler ());//Parse XML fileReader.parse ("Src/maxwell/demo.xml"); }}/*defaulthandler is the ContentHandler interface adapter * /Class Mycontenthandler extends defaulthandler{@Override     Public void startdocument()throwssaxexception {//Scan to XML document startSystem.out.println ("XML parsing begins"); }@Override     Public void enddocument()throwssaxexception {//Scan to XML document endSystem.out.println ("XML parsing End"); }@Override     Public void startelement(string uri, String localname, string name, Attributes Attributes)throwssaxexception {//Discovery start tab, print label nameSystem.out.println ("<"+name+">"); }@Override     Public void characters(Char[] ch,intStartintLengththrowssaxexception {//Print label BodySystem.out.println (NewString (CH, start, length)); }}
Using DOM4J to implement DOM parsing

The biggest disadvantage of sax parsing is not Rud, and Dom can, helpless itself JAXP performance is low, so now popular use dom4j parsing XML.
Using DOM4J to parse XML, the code implementation is simpler and more concise.

 PackageMaxwellImportJava.io.FileOutputStream;ImportOrg.dom4j.Document;ImportOrg.dom4j.DocumentHelper;ImportOrg.dom4j.Element;ImportOrg.dom4j.io.OutputFormat;ImportOrg.dom4j.io.SAXReader;ImportOrg.dom4j.io.XMLWriter; Public  class dom4jdemo {     Public Static void Main(string[] args)throwsException {//using DOM4J to get an XML readerSaxreader reader =NewSaxreader ();//Get Document ObjectDocument document = Reader.read ("Src/maxwell/demo.xml");//Get root Node objectElement root = Document.getrootelement ();//Print label BodySystem.out.println (Root.element ("Author"). GetText ()); System.out.println (Root.element ("Blog"). GetText ());//Add node and add properties, set label bodyElement Newele = documenthelper.createelement ("Add Node"); Newele.addattribute ("Test","Test"); Newele.settext ("Test Add node feature for dom4j"); Root.add (Newele);//write back the fileXMLWriter writer =NewXMLWriter (NewFileOutputStream ("Src/maxwell/demo.xml"), Outputformat.createprettyprint ());        Writer.write (document);    Writer.close (); }}

And with the function of beautifying output, after modification:

Summarize

In fact, both JAXP and DOM4J support DOM and sax parsing, but dom4j operations are simple and performance is more powerful than JAXP, but be aware that you must import the jar file to use dom4j.

Parsing XML with JAXP and dom4j

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.