Summary of several ways to parse XML by java
The first type: DOM.
The full name of DOM is Document Object Model, which is the document object model. In an application, the DOM-based XML analyzer converts an XML document into a collection of object models (usually called the DOM tree). The application implements the operation of XML document data through operations on this object model. Through the DOM interface, an application can access any part of the data in an XML document at any time. Therefore, this mechanism using the DOM interface is also called a random access mechanism.
The DOM interface provides a way to access XML document information through a hierarchical object model. These hierarchical object models form a node tree based on the XML document structure. Regardless of the type of information described in the XML document, even tabulation data, project lists or a document, the model generated using the DOM is in the form of a node tree. That is, the DOM forces the use of a tree model to access information in an XML document. Since XML is essentially a hierarchical structure, this description method is quite effective.
The random access method provided by the DOM tree brings great flexibility to the development of applications, and it can arbitrarily control the content in the entire XML document. However, since the DOM analyzer converts the entire XML document into a DOM tree and puts it in memory, the memory requirement is relatively high when the document is larger or the structure is relatively complex. Moreover, traversing trees with complex structures is also a time-consuming operation. Therefore, DOM analyzers have relatively high requirements for machine performance and their implementation efficiency is not very ideal. However, since the idea of the tree structure adopted by the DOM analyzer is consistent with the structure of the XML document, and given the convenience brought by random access, the DOM analyzer still has a wide range of useful value.
import java.io.File; 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 DomTest1 { public static void main(String[] args) throws Exception { // step 1: Get the dom parser factory (the function of the work is to create a specific parser) DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // System.out.println("class name: " + dbf.getClass().getName()); // step 2: Get the specific dom parser DocumentBuilder db = dbf.newDocumentBuilder(); // System.out.println("class name: " + db.getClass().getName()); // step3: parse an XML document and obtain the Document object (root node) Document document = db.parse(new File("candidate.xml")); NodeList list = document.getElementsByTagName("PERSON"); for(int i = 0; i < list.getLength(); i++) { Element element = (Element)list.item(i); String content = element.getElementsByTagName("NAME").item(0).getFirstChild().getNodeValue(); System.out.println("name:" + content); content = element.getElementsByTagName("ADDRESS").item(0).getFirstChild().getNodeValue(); System.out.println("address:" + content); content = element.getElementsByTagName("TEL").item(0).getFirstChild().getNodeValue(); System.out.println("tel:" + content); content = element.getElementsByTagName("FAX").item(0).getFirstChild().getNodeValue(); System.out.println("fax:" + content); content = element.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue(); System.out.println("email:" + content); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Attr; import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * Use recursively to parse any given xml document and output its contents to the command line * @author zhanglong * */ public class DomTest3 { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new File("student.xml")); //Get root element node Element root = doc.getDocumentElement(); parseElement(root); } private static void parseElement(Element element) { String tagName = element.getNodeName(); NodeList children = element.getChildNodes(); System.out.print("<" + tagName); //The NamedNodeMap object composed of all attributes of the element element needs to be judged on NamedNodeMap map = element.getAttributes(); //If the element has attributes if(null != map) { for(int i = 0; i < map.getLength(); i++) { //Get each attribute of the element Attr attr = (Attr)map.item(i); String attrName = attr.getName(); String attrValue = attr.getValue(); System.out.print(" " + attrName + "=/"" + attrValue + "/""); } } System.out.print(">"); for(int i = 0; i < children.getLength(); i++) { Node node = children.item(i); //Get the type of node short nodeType = node.getNodeType(); if(nodeType == Node.ELEMENT_NODE) { //It is an element, continue to recursively parseElement((Element)node); } else if(nodeType == Node.TEXT_NODE) { //Recursively exit System.out.print(node.getNodeValue()); } else if(nodeType == Node.COMMENT_NODE) { System.out.print("<!--"); Comment comment = (Comment)node; //Comment content String data = comment.getData(); System.out.print(data); System.out.print("-->"); } } System.out.print("</" + tagName + ">"); } }sax: The full name of SAX is Simple APIs for XML, which is the XML simple application program interface. Unlike DOM, the access mode provided by SAX is a sequential mode, which is a way to quickly read and write XML data. When an XML document is analyzed using the SAX analyzer, a series of events will be triggered and the corresponding event handling functions will be activated. The application can access the XML document through these event handling functions. Therefore, the SAX interface is also called an event-driven interface.
import java.io.File; 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; public class SaxTest1 { public static void main(String[] args) throws Exception { //step1: Obtain the SAX parser factory instance SAXParserFactory factory = SAXParserFactory.newInstance(); //step2: Get the SAX parser instance SAXParser parser = factory.newSAXParser(); //step3: Start parser.parse(new File("student.xml"), new MyHandler()); } } class MyHandler extends DefaultHandler { @Override public void startDocument() throws SAXException { System.out.println("parse began"); } @Override public void endDocument() throws SAXException { System.out.println("parse finished"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("start element"); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("finish element"); } } import java.io.File; import java.util.Stack; 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; public class SaxTest2 { public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(new File("student.xml"), new MyHandler2()); } } class MyHandler2 extends DefaultHandler { private Stack<String> stack = new Stack<String>(); private String name; private String gender; private String age; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { stack.push(qName); for(int i = 0; i < attributes.getLength(); i++) { String attrName = attributes.getQName(i); String attrValue = attributes.getValue(i); System.out.println(attrName + "=" + attrValue); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { String tag = stack.peek(); if("name".equals(tag)) { name = new String(ch, start, length); } else if("Gender".equals(tag)) { gender = new String(ch, start, length); } else if("Age".equals(tag)) { age = new String(ch, start, length); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { stack.pop(); //Indicate that the element has been parsed and needs to pop out of the stack if("Student".equals(qName)) { System.out.println("Name:" + name); System.out.println("Gender:" + Gender); System.out.println("Age:" + age); System.out.println(); } } }JDOM:
JDOM is an open source project based on a tree structure and uses pure JAVA technology to implement parsing, generating, serializing and various operations on XML documents. (http://jdom.org)
•JDOM directly serves JAVA programming. It utilizes many features of the more powerful JAVA language (method overloading, collection concepts, etc.) to effectively combine the functions of SAX and DOM.
•JDOM is a new API function to read, write and operate XML in Java language. These API functions are optimized to the maximum extent under the premise of directness, simplicity and efficiency.
jdom create xml
import java.io.FileWriter; import org.jdom.Attribute; import org.jdom.Comment; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class JDomTest1 { public static void main(String[] args) throws Exception { Document document = new Document(); Element root = new Element("root"); document.addContent(root); Comment comment = new Comment("This is my comments"); root.addContent(comment); Element e = new Element("hello"); e.setAttribute("sohu", "www.sohu.com"); root.addContent(e); Element e2 = new Element("world"); Attribute attr = new Attribute("test", "hehe"); e2.setAttribute(attr); e.addContent(e2); e2.addContent(new Element("aaa").setAttribute("a", "b") .setAttribute("x", "y").setAttribute("gg", "hh").setText("text content")); Format format = Format.getPrettyFormat(); format.setIndent(" "); // format.setEncoding("gbk"); XMLOutputter out = new XMLOutputter(format); out.output(document, new FileWriter("jdom.xml")); } }JDOM parsing xml
import java.io.File; import java.io.FileOutputStream; import java.util.List; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class JDomTest2 { public static void main(String[] args) throws Exception { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("jdom.xml")); Element element = doc.getRootElement(); System.out.println(element.getName()); Element hello = element.getChild("hello"); System.out.println(hello.getText()); List list = hello.getAttributes(); for(int i = 0 ;i < list.size(); i++) { Attribute attr = (Attribute)list.get(i); String attrName = attr.getName(); String attrValue = attr.getValue(); System.out.println(attrName + "=" + attrValue); } hello.removeChild("world"); XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent(" ")); out.output(doc, new FileOutputStream("jdom2.xml")); } }Dom4j
import java.io.FileOutputStream; import java.io.FileWriter; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class Test1 { public static void main(String[] args) throws Exception { // Create a document and set the root element node of the document: the first method// Document document = DocumentHelper.createDocument(); // // Element root = DocumentHelper.createElement("student"); // // document.setRootElement(root); // Create a document and set the root element node of the document: the second method Element root = DocumentHelper.createElement("student"); Document document = DocumentHelper.createDocument(root); root.addAttribute("name", "zhangsan"); Element helloElement = root.addElement("hello"); Element worldElement = root.addElement("world"); helloElement.setText("hello"); worldElement.setText("world"); helloElement.addAttribute("age", "20"); XMLWriter xmlWriter = new XMLWriter(); xmlWriter.write(document); OutputFormat format = new OutputFormat(" ", true); XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"), format); xmlWriter2.write(document); XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"), format); xmlWriter3.write(document); xmlWriter3.close(); } } import java.io.File; import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.DOMReader; import org.dom4j.io.SAXReader; public class Test2 { public static void main(String[] args) throws Exception { SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(new File("student2.xml")); Element root = doc.getRootElement(); System.out.println("root element: " + root.getName()); List childList = root.elements(); System.out.println(childList.size()); List childList2 = root.elements("hello"); System.out.println(childList2.size()); Element first = root.element("hello"); System.out.println(first.attributeValue("age")); for(Iterator iter = root.elementIterator(); iter.hasNext();) { Element e = (Element)iter.next(); System.out.println(e.attributeValue("age")); } System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document document = db.parse(new File("student2.xml")); DOMReader domReader = new DOMReader(); //Convert JAXP's Document to dom4j Document Document d = domReader.read(document); Element rootElement = d.getRootElement(); System.out.println(rootElement.getName()); } } import java.io.FileWriter; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class Test3 { public static void main(String[] args) throws Exception { Document document = new Document(); Element root = new Element("Contact List").setAttribute(new Attribute("Company", "Group A")); document.addContent(root); Element contactPerson = new Element("Contact"); root.addContent(contactPerson); contactPerson .addContent(new Element("Name").setText("Zhang San")) .addContent(new Element("Company").setText("Company"))) .addContent(new Element("Tel").setText("021-55556666"))) .addContent(new Element("Address") .addContent(new Element("Street").setText("5th Street")) .addContent(new Element("City").setText("Shanghai"))) .addContent(new Element("Province").setText("Shanghai"))); XMLOutputter output = new XMLOutputter(Format.getPrettyFormat() .setIndent(").setEncoding("gbk")); output.output(document, new FileWriter("contact.xml")); } }