DOM for XML-XML parsing

Source: Internet
Author: User
Tags xquery
This article introduces the basic concepts of XML parsing, and focuses on the use of jaxp dom technology for XML Document parsing.

1. XML Programming

XML programming is to perform crud operations on XML files.

So why should I use java or C/C ++ to perform crud operations on XML?
1. XML needs to be parsed as data transmission
2. XML needs to be read as the configuration file
3. as a small database, XML requires crud operations

W3C defines a set of standards (APIs) for the convenience of XML parsing)

1.1.XML parsing technology

1. XML parsing is divided into dom parsing and sax parsing.

  • Dom :( Document Object Model, that is, Document Object Model) is a method recommended by W3C to process XML.

  • Sax :( Simple API for XML) is not an official standard, but it is a de facto standard of the XML community. almost all XML parser supports it.

SAX parsing adopts the event-driven model while reading and parsing: parses a row from top to bottom to a certain element and calls the corresponding Parsing method.
DOM allocates a tree structure in memory based on the XML hierarchy to encapsulate XML labels, attributes, text, and other elements into tree node objects.

Different companies and organizations provide a parser for DOM and SAX:

  • Sun's JAXP

  • Dom4j organization's dom4j (most commonly used, such as hibernate)

  • JDom

Among them, JASP is part of J2SE, which provides DOM and SAX parser for DOM and SAX respectively.

Three types of parsing are also introduced here: dom, sax, and dom4j.

1.2.JAXP introduction

Sun provides the Java API for XML Parsing (JAXP) interface to use SAX and DOM. through JAXP, we can use any XML parser compatible with JAXP.

JAXP is a part of J2SE. it consists of javax. xml, org. w3c. dom, org. xml. sax, and its sub-packages.

In the javax. xml. parsers package, several factory classes are defined. programmers call these factory classes to obtain DOM or SAX parser objects for parsing xml documents.

2. DOM parsing of JAXP 2.1.XML DOM node tree

First, describe how JAXP parses xml dom objects. xml dom regards XML documents as a node-tree. all nodes in the tree are related to each other. You can access all nodes through this tree. You can modify or delete their content, or create new elements.

For example, the current XML document is as follows (this example is from the w3cschool online tutorial ):

     
          
   Harry Potter        J K. Rowling        
   
    2005
           
   
    29.99
       
      
          
   Everyday Italian        Giada De Laurentiis        
   
    2005
           
   
    30.00
       
      
          
   Learning XML        Erik T. Ray        
   
    2003
           
   
    39.95
       
      
          
   XQuery Kick Start        James McGovern        Per Bothner        Kurt Cagle        James Linn        Vaidyanathan Nagarajan        
   
    2003
           
   
    49.99
       
  
 

This tree starts from the root node and grows branches to the text node at the lowest level of the tree:

3. call the parse () method of the DOM parser object to parse the XML Document and obtain the Document object representing the entire Document. you can use the DOM feature to operate the entire XML Document.

2.3.JAXP DOM parsing instance:

The XML document is as follows:

 
 <班级>    
  <学生 地址="香港">        
   <名字>
    
Zhou Xiaoxing
               
   <年龄>
    
23
           
   <介绍>
    
Hard work
       
         
  <学生 地址="澳门">        
   <名字>
    
Lin Xiao
            
   <年龄>
    
25
           
   <介绍>
    
Be a good student
       
     
 
2.3.1. read XML documents

First, use the three steps described in section 2.2 to get the document object representing the entire document, and callread(Document document)The method is as follows:

// 1. create a DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // 2. obtain the DocumentBuilder object DocumentBuilder builder = factory through the factory instance. newDocumentBuilder (); // 3. specify the xml file to be parsed and return the document object Document document = builder. parse (new File ("src/myClass. xml "); read (document );

The read method is written as follows:

/*** Display all information of all students * @ param document */public static void read (Document document) {// Obtain NodeList = document by using the student's label name. getElementsByTagName ("student"); for (int I = 0; I
 
  

The final running result is as follows:

Use the javax. xml. transform. stream. StreamResult object to represent the data destination.

  • Transformer object obtained through TransformerFactory

[1] Adding elements
We can add a student subnode to the above XML, as shown below:

/*** Add student ** @ param document * @ throws Exception */public static void add (Document document) throws Exception {// create a new student node Element newStudent = document. createElement ("student"); // add the address attribute newStudent to the new student. setAttribute ("address", "San Francisco"); // create the student's subnode Element newStudent_name = document. createElement ("name"); newStudent_name.setTextContent ("James"); Element newStudent_age = document. createElement ("age"); newStudent_age.setTextContent ("25"); Element newStudent_intro = document. createElement ("Introduction"); newStudent_intro.setTextContent ("This is a good boy"); // add the child node to the student node newStudent. appendChild (newStudent_name); newStudent. appendChild (newStudent_age); newStudent. appendChild (newStudent_intro); // add the new student node to the document under the root node. getDocumentElement (). appendChild (newStudent); // update the XML document // Obtain TransformerFactory tff = TransformerFactory. newInstance (); // use TransformerFactory to obtain a Transformer tf = tff. newTransformer (); // update the current XML file tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

[2] Deleting an element
Similarly, we can delete a student node as follows:

/*** Delete the first student node ** @ param document */public static void delete (Document document) throws Exception {// first find this student, you do not need to convert it to Element Node student = document. getElementsByTagName ("student "). item (0); // use its parent node to delete student. getParentNode (). removeChild (student); // update this document TransformerFactory tff = TransformerFactory. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

[3] changing the element value
For example, you can change the name of the first student to Songjiang as follows:

/*** Change the Element name of the first student to Song Jiang ** @ param document */public static void update_name (Document document) throws Exception {Element student = (Element) document. getElementsByTagName ("student "). item (0); Element name = (Element) student. getElementsByTagName ("name "). item (0); name. setTextContent (""); // update this document TransformerFactory tff = TransformerFactory. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

[4] modifying or deleting attributes of an element

/*** Delete the attribute of the first student node ** @ param document */public static void delete_attribute (Document document) throws Exception {// first find this student Element student = (Element) document. getElementsByTagName ("student "). item (0); // delete the address attribute of student. removeAttribute ("address"); // update attributes // student. setAttribute ("address", "new address"); // update this document TransformerFactory tff = TransformerFactory. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

The above lists several examples of new elements (nodes). the general requirement is to change the age of Zhou Xiaoxing's students to 30. at this time, we need to traverse the XML document, find the corresponding node and modify it.

In addition, TransformerFactory is used for actual updates in all Update methods. Therefore, we can write these three statements as a function to avoid code redundancy, as shown below:

public static void update(Document document, String path) throws Exception {    TransformerFactory tff = TransformerFactory.newInstance();    Transformer tf = tff.newTransformer();    tf.transform(new DOMSource(document), new StreamResult(new File(path)));}
1. XML Programming

XML programming is to perform crud operations on XML files.

So why should I use java or C/C ++ to perform crud operations on XML?
1. XML needs to be parsed as data transmission
2. XML needs to be read as the configuration file
3. as a small database, XML requires crud operations

W3C defines a set of standards (APIs) for the convenience of XML parsing)

1.1.XML parsing technology

1. XML parsing is divided into dom parsing and sax parsing.

  • Dom :( Document Object Model, that is, Document Object Model) is a method recommended by W3C to process XML.

  • Sax :( Simple API for XML) is not an official standard, but it is a de facto standard of the XML community. almost all XML parser supports it.

SAX parsing adopts the event-driven model while reading and parsing: parses a row from top to bottom to a certain element and calls the corresponding Parsing method.
DOM allocates a tree structure in memory based on the XML hierarchy to encapsulate XML labels, attributes, text, and other elements into tree node objects.

Different companies and organizations provide a parser for DOM and SAX:

  • Sun's JAXP

  • Dom4j organization's dom4j (most commonly used, such as hibernate)

  • JDom

Among them, JASP is part of J2SE, which provides DOM and SAX parser for DOM and SAX respectively.

Three types of parsing are also introduced here: dom, sax, and dom4j.

1.2.JAXP introduction

Sun provides the Java API for XML Parsing (JAXP) interface to use SAX and DOM. through JAXP, we can use any XML parser compatible with JAXP.

JAXP is a part of J2SE. it consists of javax. xml, org. w3c. dom, org. xml. sax, and its sub-packages.

In the javax. xml. parsers package, several factory classes are defined. programmers call these factory classes to obtain DOM or SAX parser objects for parsing xml documents.

2. DOM parsing of JAXP 2.1.XML DOM node tree

First, describe how JAXP parses xml dom objects. xml dom regards XML documents as a node-tree. all nodes in the tree are related to each other. You can access all nodes through this tree. You can modify or delete their content, or create new elements.

For example, the current XML document is as follows (this example is from the w3cschool online tutorial ):

       
            
     Harry Potter        J K. Rowling        
     
      2005
             
     
      29.99
         
        
            
     Everyday Italian        Giada De Laurentiis        
     
      2005
             
     
      30.00
         
        
            
     Learning XML        Erik T. Ray        
     
      2003
             
     
      39.95
         
        
            
     XQuery Kick Start        James McGovern        Per Bothner        Kurt Cagle        James Linn        Vaidyanathan Nagarajan        
     
      2003
             
     
      49.99
         
    
   

This tree starts from the root node and grows branches to the text node at the lowest level of the tree:

3. call the parse () method of the DOM parser object to parse the XML Document and obtain the Document object representing the entire Document. you can use the DOM feature to operate the entire XML Document.

2.3.JAXP DOM parsing instance:

The XML document is as follows:

   
   <班级>    
    <学生 地址="香港">        
     <名字>
      
Zhou Xiaoxing
                 
     <年龄>
      
23
             
     <介绍>
      
Hard work
         
           
    <学生 地址="澳门">        
     <名字>
      
Lin Xiao
              
     <年龄>
      
25
             
     <介绍>
      
Be a good student
         
       
   
2.3.1. read XML documents

First, use the three steps described in section 2.2 to get the document object representing the entire document, and callread(Document document)The method is as follows:

// 1. create a DocumentBuilderFactoryDocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); // 2. obtain the DocumentBuilder object DocumentBuilder builder = factory through the factory instance. newDocumentBuilder (); // 3. specify the xml file to be parsed and return the document object Document document = builder. parse (new File ("src/myClass. xml "); read (document );

The read method is written as follows:

/*** Display all information of all students * @ param document */public static void read (Document document) {// Obtain NodeList = document by using the student's label name. getElementsByTagName ("student"); for (int I = 0; I
   
    

The final running result is as follows:

Use the javax. xml. transform. stream. StreamResult object to represent the data destination.

  • Transformer object obtained through TransformerFactory

[1] Adding elements
We can add a student subnode to the above XML, as shown below:

/*** Add student ** @ param document * @ throws Exception */public static void add (Document document) throws Exception {// create a new student node Element newStudent = document. createElement ("student"); // add the address attribute newStudent to the new student. setAttribute ("address", "San Francisco"); // create the student's subnode Element newStudent_name = document. createElement ("name"); newStudent_name.setTextContent ("James"); Element newStudent_age = document. createElement ("age"); newStudent_age.setTextContent ("25"); Element newStudent_intro = document. createElement ("Introduction"); newStudent_intro.setTextContent ("This is a good boy"); // add the child node to the student node newStudent. appendChild (newStudent_name); newStudent. appendChild (newStudent_age); newStudent. appendChild (newStudent_intro); // add the new student node to the document under the root node. getDocumentElement (). appendChild (newStudent); // update the XML document // Obtain TransformerFactory tff = TransformerFactory. newInstance (); // use TransformerFactory to obtain a Transformer tf = tff. newTransformer (); // update the current XML file tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

[2] Deleting an element
Similarly, we can delete a student node as follows:

/*** Delete the first student node ** @ param document */public static void delete (Document document) throws Exception {// first find this student, you do not need to convert it to Element Node student = document. getElementsByTagName ("student "). item (0); // use its parent node to delete student. getParentNode (). removeChild (student); // update this document TransformerFactory tff = TransformerFactory. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

[3] changing the element value
For example, you can change the name of the first student to Songjiang as follows:

/*** Change the Element name of the first student to Song Jiang ** @ param document */public static void update_name (Document document) throws Exception {Element student = (Element) document. getElementsByTagName ("student "). item (0); Element name = (Element) student. getElementsByTagName ("name "). item (0); name. setTextContent (""); // update this document TransformerFactory tff = TransformerFactory. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

[4] modifying or deleting attributes of an element

/*** Delete the attribute of the first student node ** @ param document */public static void delete_attribute (Document document) throws Exception {// first find this student Element student = (Element) document. getElementsByTagName ("student "). item (0); // delete the address attribute of student. removeAttribute ("address"); // update attributes // student. setAttribute ("address", "new address"); // update this document TransformerFactory tff = TransformerFactory. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new File ("src/myClass. xml ")));}

The above lists several examples of new elements (nodes). the general requirement is to change the age of Zhou Xiaoxing's students to 30. at this time, we need to traverse the XML document, find the corresponding node and modify it.

In addition, TransformerFactory is used for actual updates in all Update methods. Therefore, we can write these three statements as a function to avoid code redundancy, as shown below:

public static void update(Document document, String path) throws Exception {    TransformerFactory tff = TransformerFactory.newInstance();    Transformer tf = tff.newTransformer();    tf.transform(new DOMSource(document), new StreamResult(new File(path)));}

The above is the content of DOM of XML-XML resolution, more relevant content please pay attention to PHP Chinese network (www.php1.cn )!

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.