I. Overview
XML is all called Extensible Markup Language. Used primarily for describing data and for use as a configuration file.
The XML document is logically composed of 5 parts:
- xml declaration: Indicates the version of the XML used, Document encoding, document independence information
-
-
- Note: start with <!--to-- > End for a description of the contents of the document
- Processing instructions: Processing instructions to notify other applications to process non-XML format data in the format <?xml-stylesheet href= "Hello.css" type= "Text/css"?
The root element of an XML document is called a document element, and it is a child of the document entity, as well as the processing instructions, comments, etc. that appear outside it, and the root element itself and the child elements inside it are also a tree.
II. parsing of XML documents
When parsing an XML document, the XML document is usually parsed using an existing XML parser, and the application obtains the XML data through the API interface provided by the parser.
There are two ways to parse XML: Dom and sax:
DOM: Used to parse a relatively small XML file, easy to delete and change the search. At the heart of the DOM is the node, which, when parsing an XML document, maps the parts that make up the document to an object, which is called a node. Parsing an XML document using the DOM requires that you read the entire XML document and then create the DOM tree in memory to generate each node object on the DOM tree.
<?xml version= "1.0" encoding= "UTF-8"?>< Bookshelf > < book > < author > li </author > < price > 39 USD </Price > < Press > Higher Education Press </Press > </book > < > < author > Chiao </author > < price >40 Yuan </price > < publisher > Publishing house </Press > </book ></bookshelf >
Use the DOM to parse the above XML document with the following code:
Package Com.test.xml;import Java.io.file;import Java.io.ioexception;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 demo{public static void Main (String args[]) {//Get an instance of the DOM parser factory class Documentbuilderfactory dbf=documentbuilderfactory.newinstance (); try {//Get DOM parser object Documentbuilder Db=dbf.newdocumentbuilder (); Parse the XML document and get the Document object file File=new file ("D:\\eclipse\\workspace\\day_050401\\src\\book.xml") representing the documents; Document doc=db.parse (file); Returns the label name for all descendant elements of the book in document order NodeList Nl=doc.getelementsbytagname ("book"); for (int i=0;i<nl.getlength (); i++) {element elt= (Element) Nl.item (i); Node Eltauthor=elt.getelementsbytagname ("author"). Item (0); Node eltpricer=elt.getelementsbytagname ("price"). Item (0); Node Eltpublish=elt.getelementsbytagname ("publisher"). Item (0); String author=eltauthor.getfirstchild (). Getnodevalue (); String pricer=eltpricer.getfirstchild (). Getnodevalue (); String publish=eltpublish.getfirstchild (). Getnodevalue (); System.out.println ("-------Book Information" + (i+1) + "-------"); System.out.println ("Author:" +author); System.out.println ("Price:" +pricer); System.out.println ("Publishing house:" +publish); }} catch (Parserconfigurationexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (Saxexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODOAuto-generated catch block E.printstacktrace (); } }}
The results of the implementation are as follows:
SAX: Memory consumption is small and suitable for read operations. Sax is an event-driven API that parses XML documents with sax involving two parts of parsers and event handlers. The parser is responsible for reading the XML document and sending events to the event handler, which is responsible for handling the event and processing the passed XML data.
Parse the XML document using Sax, with the following code:
Import Java.io.file;import java.io.ioexception;import Java.util.arraylist;import Java.util.list;import Javax.xml.parsers.parserconfigurationexception;import Javax.xml.parsers.saxparser;import Javax.xml.parsers.saxparserfactory;import Org.xml.sax.attributes;import Org.xml.sax.saxexception;import Org.xml.sax.xmlreader;import org.xml.sax.helpers.defaulthandler;class book{private String name; Private String author; Private String Price; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public String Getauthor () {return author; } public void Setauthor (String author) {this.author = author; } public String GetPrice () {return price; The public void Setprice (String price) {this.price = Price; }}public class Demo extends defaulthandler{private List list=new ArrayList (); Private String Currenttag; Private book book;@Override public void Startelement (string uri, String localname, string name, Attributes Attributes) throws saxexception {currenttag=name; if ("book". Equals (Currenttag)) {book=new books (); }} @Override public void characters (char[] ch, int start, int length) throws Saxexception { if ("publisher". Equals (Currenttag)) {string Name=new string (ch,start,length); Book.setname (name); } if ("Author". Equals (Currenttag)) {string Author=new string (ch,start,length); Book.setauthor (author); if ("Price". Equals (Currenttag)) {string Price=new string (ch,start,length); Book.setprice (price); }} @Override public void EndElement (string uri, String localname, String name) throws Saxexception {if (Name.equals (")") {List.add (book); Book=null; } Currenttag=null; } public List Getbooks () {return list; The public static void main (String []args) {//1. Create parse Factory saxparserfactory Factory=saxparserfactory.newins Tance (); SAXParser Sp=null; try {//2. Get parser Sp=factory.newsaxparser (); 3, Get Reader XMLReader reader=sp.getxmlreader (); File File=new file ("D:\\eclipse\\workspace\\day_050401\\src\\book.xml"); 4. Set Content Processor Demo Handle=new demo (); Reader.setcontenthandler (handle); Sp.parse (File,handle); 5. Read the XML document content list<book> List=handle.getbooks (); for (int i=0;i<list.size (); i++) System.out.println (List.get (i). Getauthor () + "----" +list.get (i). GetName () + "---- -"+list.get (i). GetPrice ()); } catch (Parserconfigurationexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (SAXEXception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } }}
The results of the operation are as follows:
Iii. dom4j Parsing XML documents
DOM4J is also a Java library that parses open source code for XML documents. http://sourceforge.net/projects/dom4j/.
Use DOM4J to read the XML document operation with the following code:
Import Java.io.fileoutputstream;import Java.io.filewriter;import Java.io.ioexception;import Java.io.outputstreamwriter;import Java.security.keystore.entry.attribute;import Org.dom4j.Document;import Org.dom4j.documentexception;import Org.dom4j.element;import Org.dom4j.io.outputformat;import Org.dom4j.io.saxreader;import Org.dom4j.io.xmlwriter;import Org.junit.test;public class Demo{//Read the XML file The second book of the publishing house @Tes T public void Read () {Saxreader reader = new Saxreader (); try {Document document = Reader.read ("C:\\users\\administrator\\desktop\\book.xml"); Element root =document.getrootelement (); Element book= (Element) root.elements ("book"). Get (1); String value=book.element ("publisher"). GetText (); System.out.println (value); } catch (Documentexception e) {//TODO auto-generated catch block E.printstacktrace (); }}//Add a title to the second book:< title > Ordinary World </title > @Test PubLIC void Add () throws Documentexception, IOException {saxreader reader = new Saxreader (); Document document = Reader.read ("C:\\users\\administrator\\desktop\\book.xml"); Element book= (Element) document.getrootelement (). Elements ("book"). Get (1); Book.addelement ("title"). SetText ("The Ordinary World"); Update memory XMLWriter writer = new XMLWriter (New OutputStreamWriter ("C:\\users\\administrator\\desk Top\\book.xml ")," UTF-8 "); Writer.write (document); Writer.close (); }}
Operation Result:
PS: If your project often needs to replace the parser, it is recommended to use DOM and sax, so when the parser is replaced without changing any code, if there is no such requirement, it is recommended to use DOM4J, simple and powerful.
Basic use of XML