XML has now become a general Data Interchange format, its platform independent, language-independent, system-independent, which brings great convenience to data integration and interaction. Today is mainly about four methods of parsing XML that are commonly used in Java.
Suppose the content and structure of our XML are as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<HD>
<disk name= "C" >
<capacity>4g</ capacity>
<directories>100</directories>
<files>1580</files>
</disk >
<disk name= "D" >
<capacity>10G</capacity>
<directories>300</ directories>
<files>1500</files>
</disk>
</HD>
A. DOM
Using DOM to parse an XML file is simpler, and its parsing process is to read the XML file into memory, create a document tree, and manipulate the XML file by manipulating the document tree.
Advantages: The entire document tree in memory, easy to operate, support delete, modify, rearrange and so on a variety of functions;
Disadvantage: The entire document is transferred into memory (including unwanted nodes). Waste of time and space; use occasions: once the document has been parsed to access the data more than once, hardware resources (memory, CPU), and the operation of data to do a lot of traversal tree operations, which for a large number of data access, modify the operating rate is not high, However, for the general application of small data, it is enough to deal with DOM.
Dom parsing:
Package DOM;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import org.w3c.dom.NodeList; public class Domxml {public static void main (string[] args) throws exception{//factory instance with DOM parser Documentbuilderfacto
Ry Dbfactory=documentbuilderfactory.newinstance ();
Get DOM parser Documentbuilder Dbbuilder=dbfactory.newdocumentbuilder () from the DOM factory;
Declared as file to identify the Chinese name Document document=null;
Document=dbbuilder.parse ("D:/app/analysisxml/src/dom/test.xml");
Gets the node list for the element with the document name Test nodelist list =document.getelementsbytagname ("Disk");
Iterates through the collection, showing the name of the element and its child elements in the binding for (int i=0;i<list.getlength (); i++) {element element= (Element) List.item (i);
String Name=element.getattribute ("name");
String capacity=element.getelementsbytagname ("Capacity"). Item (0). Getfirstchild (). Getnodevalue (); String directories=element.getelementsbytagname ("directories"). Item (0). gEtfirstchild (). Getnodevalue ();
String files=element.getelementsbytagname ("Files"). Item (0). Getfirstchild (). Getnodevalue ();
SYSTEM.OUT.PRINTLN ("Disk information:");
SYSTEM.OUT.PRINTLN ("section letter:" +name);
SYSTEM.OUT.PRINTLN ("Partition Capacity:" +capacity);
SYSTEM.OUT.PRINTLN ("Directory number:" +directories);
System.out.println ("Number of documents:" +files);
System.out.println ("-----------------------------------");
}
}
}
two. SAX
Sax is the abbreviation for the simple API for XML, which is not an official standard, but a standard generated by online community discussions, and is supported by almost all XML parsers. Sax refers to an interface, or a package. Also known as the XML simple application interface. Unlike DOM, the access pattern that sax provides is a sequential pattern, a way to quickly read and write XML data, parse XML files with sax, You do not need to read the document into memory. When parsing an XML document using a SAX parser, triggers a series of events and activates the appropriate event handlers that the application uses to access the XML document, so the work that the user needs to do is to add the program code to the corresponding event method. So the Sax interface is also called a thing Components drive interface.
Sax parsing:
Test.java
Package DOM;
public class Test {
private String name;
private String capacity;
Private String directories;
Private String files;
Public String Getname ()
{return
name;
}
public void SetName (String name)
{
this.name=name
}
Public String getcapacity ()
{return
capacity;
}
public void setcapacity (String capacity)
{
this.capacity=capacity;
}
Public String GetDirectories ()
{return
directories;
}
public void Setdirectories (String directories)
{
this.directories=directories;
}
Public String GetFiles ()
{return
files;
}
public void Setfiles (String files)
{
this.files=files
}
}
SAX interprets XML:
Package DOM;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
Import Dom.test;
public class Saxxmltest {public static void main (string[] args) throws Exception {//Parser acquisition
1. Get Sax parsing factory saxparserfactory SF = Saxparserfactory.newinstance ();
2, get the parser SAXParser SP = Sf.newsaxparser (); Create an input stream for an XML file InputStream instream = SaxParseService.class.getClassLoader (). getResourceAsStream ("Dom/test.xml"
);
3, parsing the XML file, the event handler passed in.
Sp.parse (instream, New Myhander ());
} class Myhander extends defaulthandler{private list<test> hd=null;
Private test disk=null; Private String Pretag =null; function is the last node name when the record is resolved public VOID startdocument () throws Saxexception {hd=new arraylist<test> ();
public void Startelement (String uristring,string localname,string qname,attributes Attributes) throws saxexception{
if ("Disk". Equals (QName)) {disk=new test (); Disk.
SetName (attributes.getvalue (0));
} pretag=qname; public void characters (char[] ch,int start,int length) throws saxexception{if (pretag!=null) {String Contentstrin
G=new String (ch,start,length); if ("Capacity". Equals (Pretag)) {disk.
Setcapacity (contentstring); else if ("directories". Equals (Pretag)) {disk.
Setdirectories (contentstring); }else if ("Files". Equals (Pretag)) {disk.
Setfiles (contentstring); }} public void EndElement (String uri,string localname,string qName) throws saxexception{if ("Disk". Equals (QName))
{hd.add (disk);
Disk=null;
} pretag=null; public void Enddocument () throws Saxexception {for (test Disk:hd) {SYSTEM.OUT.PR InTLN ("Disk information:"); SYSTEM.OUT.PRINTLN ("Partition Letter:" +disk.)
Getname ()); SYSTEM.OUT.PRINTLN ("Partition Capacity:" +disk.)
Getcapacity ()); System.out.println ("Number of directories:" +disk.)
GetDirectories ()); System.out.println ("Number of files:" +disk.)
GetFiles ());
System.out.println ("-----------------------------------");
}
}
}
three. dom4j
Dom4j is a very, very good Java XML API with excellent performance, powerful features and extreme ease of use, and it is also an open-source software. DOM4J's development package contains a number of third-party development packages, and when calling dom4j some features need to add a corresponding development package, most of the time, the basic Dom4j.jar can be imported into the project.
DOM4J Resolution:
Package DOM;
Import Java.util.Iterator;
Import Org.apache.naming.java.javaURLContextFactory;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader; public class Dom4jxml {public static void main (string[] args) throws exception{//Create file Object Java.io.File file=new ja
Va.io.File ("D:/app/analysisxml/src/dom/test.xml");
Creates an object that reads an XML file Saxreader reader=new Saxreader ();
Create a Document Object Document=reader.read (file);
Gets the root node Element element=document.getrootelement () of the file;
For (iterator I=element.elementiterator ("Disk"); I.hasnext ();) {//get node element elements = (element) I.next ();
String name=element.attributevalue ("name"); String Capacity=element.elementtext ("Capacity"), or the content string that takes the child element capacity of the disk Directories=element.elementte
XT ("directories");
String Files=element.elementtext ("files");
SYSTEM.OUT.PRINTLN ("Disk information:");
SYSTEM.OUT.PRINTLN ("section letter:" +name); SYSTEM.OUT.PRINTLN ("Partition Capacity:" +capacity);
SYSTEM.OUT.PRINTLN ("Directory number:" +directories);
System.out.println ("Number of documents:" +files);
System.out.println ("-----------------------------------");
}
}
}
four. JDOM
Jdom parsing XML is based on a tree structure, which utilizes pure Java technology to implement various operations on XML documents. Jdom can be said to serve Java programming directly, combining SAX and DOM functions effectively. The length of the Jdom application is usually one-third of the DOM application, and is about the average for sax applications.
Advantages: 20-80 principles, greatly reducing the amount of code.
Usage: Simple to implement, such as parsing, creating, but at the bottom, Jdom still uses sax (most commonly used), DOM, Xanan documents.
Jdom parsing xml:
Package DOM;
Import java.util.List;
Import Javax.servlet.jsp.PageContext;
Import org.jdom.*;
Import org.jdom.input.*;
public class Jdomxml {public static void main (string[] args) throws exception{Saxbuilder sbuilder=new Saxbuilder (); Document document=sbuilder.build ("Src/dom/test.xml"); Constructs the Document object element Rootelement=document.getrootelement ()//Gets the root element list<element> List=rootelement.getchildren ("D ISK ")//Get all the elements named disk for (int i=0;i<list.size (); i++)//traverse the collection of child elements of the root element {element element= (element) List.get (
i);
String name=element.getattributevalue ("name"); String Capacity=element.getchildtext ("Capacity"), or the content string that takes the child element capacity of the disk Directories=element.getchildtex
T ("directories");
String Files=element.getchildtext ("files");
SYSTEM.OUT.PRINTLN ("Disk information:");
SYSTEM.OUT.PRINTLN ("section letter:" +name);
SYSTEM.OUT.PRINTLN ("Partition Capacity:" +capacity); System.out.println ("Number of directories:" +directoRIES);
System.out.println ("Number of documents:" +files);
System.out.println ("-----------------------------------");
}
}
}
Five. Analytical Results
Six. Summary
XML is interpreted in the same way in different languages, except that the syntax of the implementation is different. XML parsing, you can more own size of the data, choose different types of resolution, while improving the efficiency of parsing!