Summary of several methods for parsing XML in java

Source: Internet
Author: User
This article summarizes four methods for parsing XML in java, which are very detailed in combination with specific examples. if you need this method, you can refer to several methods for parsing XML in java.

First: DOM.

The full name of DOM is Document Object Model, that is, 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 operates on this object model exactly, to perform operations on XML document data. Through the DOM interface, an application can access any part of the data in the XML document at any time. Therefore, this mechanism using the DOM interface is also called random access mechanism.

The DOM interface provides a way to access XML document information through hierarchical object models. 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 list, or document, the model generated by using DOM is in the form of a node tree. That is to say, DOM forces the use of tree models to access information in XML documents. Because XML is essentially a layered structure, this description method is quite effective.

The random access method provided by the DOM tree provides great flexibility for application development. it can control the content of the entire XML document at will. However, because the DOM analyzer converts the entire XML document into a DOM tree in the memory, the memory requirement is high when the document is large or the structure is complex. In addition, traversing trees with complex structures is also a time-consuming operation. Therefore, DOM analyzer has high requirements on machine performance and is inefficient at implementation. However, because the structure of the tree used by the DOM analyzer is consistent with the structure of the XML document and the convenience of random access, DOM analyzer is still widely used.

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: obtain the dom parser factory (which is used to create a specific parser) DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); // System. out. println ("class name:" + dbf. getClass (). getName (); // step 2: obtain the specific dom parser DocumentBuilder db = dbf. newDocumentBuilder (); // System. out. println ("class name:" + db. getClass (). getName (); // Step 3: 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 recursion to parse any given xml document and output its content 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 "); // Obtain the 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); // NamedNodeMap object composed of all the attributes of the element, which must be judged by NamedNodeMap map = element. getAttributes (); // if the element has an attribute if (null! = Map) {for (int I = 0; I <map. getLength (); I ++) {// Obtain each attribute of this 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); // Obtain the node type short nodeType = node. getNodeType (); if (nodeType = Node. ELEMENT_NODE) {// is an Element. continue recursion of parseElement (Element) node);} else if (nodeType = Node. TEXT_NODE) {// recursively exports System. out. print (node. getNodeValue ();} else if (nodeType = Node. COMMENT_NODE) {System. out. print ("
 ") ;}} System. out. print ("
 ");}}

Sax: The full name of SAX is Simple APIs for XML, that is, Simple XML application interface. Unlike DOM, the access mode provided by SAX is a sequential mode, which allows you to quickly read and write XML data. When you use the SAX analyzer to analyze XML documents, a series of events are triggered and corresponding event processing functions are activated. applications can use these event processing functions to access XML documents, 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: Get the SAXParserFactory factory = SAXParserFactory of the SAX parser factory. newInstance (); // Step 2: obtain the SAXParser parser = factory. newSAXParser (); // step3: start parsing 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
 
  
Stack = new Stack
  
   
(); 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 (); // indicates that the element has been parsed. if ("student" needs to pop up from the stack ". 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 that uses the pure JAVA technology to parse, generate, serialize, and operate XML documents based on the tree structure. Http://jdom.org)

• JDOM directly serves JAVA programming. It makes use of the many features of the more powerful JAVA language (Method overloading, set concepts, etc.) to effectively combine the functions of SAX and DOM.

• JDOM is a new API function that reads, writes, and operates XML in Java. These API functions are optimized to the maximum extent on the premise of direct, simple, and efficient.

Create xml using jdom

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"));         } }

Parse xml using JDOM

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. export enthelper; 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: method 1 // Document document = incluenthelper. createDocument (); // Element root = incluenthelper. createElement ("student"); // document. setRootElement (root); // create a document and set the root Element node of the document. Method 2: Element root = incluenthelper. createElement ("student"); Document document = incluenthelper. 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 dbf = DocumentBuilderFactory. newInstance (); DocumentBuilder db = dbf. newDocumentBuilder (); org. w3c. dom. document document = db. parse (new File ("student2.xml"); DOMReader domReader = new DOMReader (); // Convert the Document of JAXP to the Document d = domReader of dom4j. 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", "A group"); 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 ("phone "). setText ("021-55556666 ")). addContent (new Element ("address "). addContent (new Element ("street "). setText ("5 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 "));}}

For more articles about how to parse XML in java, refer to the PHP Chinese website!

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.