Java Parsing XML file

Source: Internet
Author: User

Why use XML as a data exchange? Why parse XML with Java? Why is it different from parsing xml?dom and sax parsing with dom4j?

With this problem to learn, you find that this learning is the most efficient

First question: Why use XML as a data exchange

First of all, XML is a platform-free, language-free, no-system limitation. Make the XML can be written in different languages of the system to exchange data, the requirements between different languages written by the system will not change, may be XML such transfer of data media will be replaced by other. Like JSON, It is possible that JSON will also be replaced. The way changes. But the demand is not changing. And the structure of the XML tree node satisfies the SOAP (Simple Object Access Protocol) Trivial objects Access protocol.

Second question: Why use Java to parse XML

You can use any language, this is not a requirement.

The third question: Why parse XML with DOM4J

XML parsing in Java has DOM parsing and sax parsing, and Dom parsing has dom4j and jdom parsing, both of which are poor. Use which one to look at individual needs. (answer on question fourth);

Question fourth: What is the difference between DOM parsing and sax parsing?

Dom parsing adds the entire document tree to memory, and sax is event-driven. The callback function is invoked when an event is encountered. The XML file content is relatively large. Dom parsing is slow and sax is faster.

Useful dom4j Creating XML code

Privatedocument document; @Override Public voidcreatexml (String fileName) {//Get Document         This. Document =documenthelper.createdocument (); //Creating the root nodeElement employees = document.addelement ("Employees"); //Create next nodeElement em1 = employees.addelement ("Employee"); Element name1= Em1.addelement ("name"); Element Age1= Em1.addelement ("Age"); Element em2= Employees.addelement ("Employee"); Element name2= Em2.addelement ("name"); Element Age2= Em2.addelement ("Age"); //to create a property of a node elementName1.settext ("Jack"); Age1.settext ("20"); Name2.settext ("Amy"); Age2.settext ("30"); //Create a Stream        Try{XMLWriter XMLWriter=NewXMLWriter (NewFileOutputStream (NewFile (fileName));        Xmlwriter.write (document); } Catch(IOException e) {e.printstacktrace (); }        //write it out .}

Practical DOM4J Parsing XML code

@Override Public voidpraserxml (String fileName) {File xmlfile=NewFile (fileName); Saxreader Sax=NewSaxreader (); Try{document=Sax.read (xmlfile); Element Root=document.getrootelement ();  for(Iterator<element> ite =root.elementiterator (); Ite.hasnext ();) {element element=Ite.next ();  for(Iterator<element> it =element.elementiterator (); It.hasnext ();) {element Ele=It.next (); System.out.println (Ele.getname ()+ "--" +Ele.gettext ()); }            }        } Catch(documentexception e) {e.printstacktrace (); }            }

Java Parsing XML file

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.