Summary of several ways Java parses XML

  • 2020-04-01 04:37:13
  • OfStack

Summary of several ways Java parses XML

The first: DOM.

The full name for DOM is the Document Object Model, or Document Object Model. In an application, a dom-based XML parser converts an XML document into a collection of object models (commonly known as DOM trees), and it is through this object model that the application manipulates the XML document data. Through a DOM interface, an application can access any part of the data in an XML document at any time, so this mechanism that leverages the DOM interface is also known as a random access mechanism.

The DOM interface provides a way to access XML document information through a hierarchical object model that forms a tree of nodes based on the document structure of the XML. No matter what type of information is described in an XML document, even if it is tabular data, a list of items, or a document, the model generated using the DOM is in the form of a node tree. That is, DOM enforces the use of a tree model to access information in an XML document. Because XML is essentially a hierarchical structure, this approach is quite effective.

The random access provided by the DOM tree gives the development of the application a great deal of flexibility, allowing it to arbitrarily control the contents of the entire XML document. However, because the DOM parser converts the entire XML document into a DOM tree and places it in memory, memory requirements are high when the document is large or complex. Furthermore, traversing a complex tree is a time-consuming operation. Therefore, DOM analyzer requires high performance of the machine, and the implementation efficiency is not ideal. However, because the idea of tree structure adopted by DOM parsers is consistent with the structure of XML documents, and because of the convenience of random access, DOM parsers can be 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: get the dom parser factory (the job is to create a concrete 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 to 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; 
 
 
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")); 
    //Gets 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); 
     
    //The NamedNodeMap object made up of all the attributes of the element element needs to be judged
    NamedNodeMap map = element.getAttributes(); 
     
    //If the element has attributes
    if(null != map) 
    { 
      for(int i = 0; i < map.getLength(); i++) 
      { 
        //Gets 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); 
      //Gets the type of node
      short nodeType = node.getNodeType(); 
       
      if(nodeType == Node.ELEMENT_NODE) 
      { 
        //That's the element. So let's keep recursing
        parseElement((Element)node); 
      } 
      else if(nodeType == Node.TEXT_NODE) 
      { 
        //Recursive export
        System.out.print(node.getNodeValue()); 
      } 
      else if(nodeType == Node.COMMENT_NODE) 
      { 
        System.out.print("<!--"); 
         
        Comment comment = (Comment)node; 
         
        //The comment
        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, or XML Simple application program interface. Unlike DOM, SAX provides a sequential access pattern, which is a quick way to read and write XML data. When a SAX parser is used to analyze an XML document, a series of events are triggered and the corresponding event handlers are activated. These event handlers are used by the application program to access the XML document, so 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 SAX parser factory instance
    SAXParserFactory factory = SAXParserFactory.newInstance(); 
     
    //Step2: get the SAX parser instance
    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<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(" The 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 and needs to be ejected from the stack
     
    if(" students ".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 pure JAVA techniques to parse, generate, serialize, and manipulate XML documents based on tree structures. ((link: http://jdom.org)

The & # 8226; JDOM serves JAVA programming directly. It leverages the many features of the more powerful JAVA language (method overloading, collection concepts, and so on) to effectively combine the capabilities of SAX and DOM.

The & # 8226; JDOM is a new API for reading, writing, and manipulating XML in the Java language. These API functions are optimized to the maximum extent possible while being direct, simple, and efficient.

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 to parse the 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 document's root element node: the first way
    // Document document = DocumentHelper.createDocument(); 
    // 
    // Element root = DocumentHelper.createElement("student"); 
    // 
    // document.setRootElement(root); 
 
    //Create a document and set the document's root element node: the second way
    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 dbf = 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's 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(" The company ", 
        "A group ")); 
 
    document.addContent(root); 
     
    Element contactPerson = new Element(" The contact "); 
     
    root.addContent(contactPerson); 
 
    contactPerson 
        .addContent(new Element(" The name ").setText(" Zhang SAN ")) 
        .addContent(new Element(" The company ").setText("A The company ")) 
        .addContent(new Element(" The phone ").setText("021-55556666")) 
        .addContent( 
            new Element(" address ") 
                .addContent(new Element(" The street ").setText("5 Street, ")) 
                .addContent(new Element(" city ").setText(" Shanghai ")) 
                .addContent(new Element(" provinces ").setText(" Shanghai "))); 
 
    XMLOutputter output = new XMLOutputter(Format.getPrettyFormat() 
        .setIndent("  ").setEncoding("gbk")); 
 
    output.output(document, new FileWriter("contact.xml")); 
 
  } 
} 

Related articles: