dom4j parses XML

Posted by dhe on Wed, 22 May 2019 23:57:19 +0200

1. Parsing xml using dom4j

* dom4j is an organization that provides parser dom4j for xml parsing


* dom4j is not part of javase. What do you need to do to use the first step?
*** Import dom4j to provide jar packages
Create a folder lib
Copy the jar package under lib.
Right-click on the jar package, build path -- add to build path
- See the jar package and turn it into a bottle, indicating that the import is successful.

* Get the document
SAXReader reader = new SAXReader();
        Document document = reader.read(url);
* document's parent interface is Node
* If you can't find the method you want in the document, go to Node and find it.


* The method getRootElement() in document: Getting the root node returns Element


* Element is also an interface, and the parent interface is Node
- Element and Node inner methods
** getParent(): Get the parent node
** addElement: Add Tags


* element(qname)
** Represents the first sublabel under the capture label
** qname: the name of the tag
* elements(qname)
** Get all the sub-labels of the name below the label (layer 1)
** qname: label name
* elements()

** Get all the sub-labels below the label

1.1TestDom4j.java

  1. package cn.itcast.dom4jtest;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.dom4j.Document;  
  6. import org.dom4j.DocumentHelper;  
  7. import org.dom4j.Element;  
  8. import org.dom4j.io.SAXReader;  
  9.   
  10. import cn.itcast.utils.Dom4jUtils;  
  11.   
  12.   
  13. public class TestDom4j {  
  14.   
  15.     public static void main(String[] args) throws Exception {  
  16. //      selectName();  
  17. //      selectSin();  
  18. //      selectSecond();  
  19. //      addSex();  
  20. //      addAgeBefore();  
  21. //      modifyAge();  
  22. //      delSch();  
  23.         getValues();  
  24.     }  
  25.       
  26.     //Get the value of attribute id1 in the first p1  
  27.     public static void getValues() throws Exception {  
  28.         /* 
  29.          * 1,Get the document 
  30.          * 2,Get the root node 
  31.          * 3,Get the first p1 element 
  32.          * 4,Get the attribute values in p1 
  33.          * */  
  34.         //Get the document  
  35.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  36.         //Get the root node  
  37.         Element root = document.getRootElement();  
  38.         //Get the first p1 element  
  39.         Element p1 = root.element("p1");  
  40.         //Get the attribute values in p1  
  41.         String value = p1.attributeValue("id1");  
  42.         System.out.println(value);  
  43.     }  
  44.       
  45.     //Delete the < School > ECIT </school > element under the first p1  
  46.     public static void delSch() throws Exception {  
  47.         /* 
  48.          * 1,Get the document 
  49.          * 2,Get the root node 
  50.          * 3,Get the first p1 tag 
  51.          * 4,Get the school element under the first p1 
  52.          * 5,Delete (use p1 to delete school) 
  53.          *  
  54.          * 6,Write back xml 
  55.          * */  
  56.         //Get the document ctrl shift o Quick Guide  
  57.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  58.         //Get the root node  
  59.         Element root = document.getRootElement();  
  60.         //Get the first p1 element  
  61.         Element p1 = root.element("p1");  
  62.         //Get the school label under p1  
  63.         Element sch = p1.element("school");  
  64.         //Delete the school element  
  65.         //Delete by parent node  
  66.         //Method of Getting Parent Node  
  67.         //sch.getParent(); // Gets the parent node p1 of school  
  68.         p1.remove(sch);  
  69.         //Write back xml  
  70.         Dom4jUtils.xmlWriters(Dom4jUtils.PATH, document);  
  71.     }  
  72.       
  73.     //Modify the value of age element under the first p1 < age > 30 </age >  
  74.     public static void modifyAge() throws Exception {  
  75.         /* 
  76.          * 1,Get the document 
  77.          * 2,Get the root node, and then get the first p1 element 
  78.          * 3,Get the age below the first p1 
  79.          * 4,The modified value is 30 
  80.          *  
  81.          * 5,Write back xml 
  82.          *  
  83.          * */  
  84.         //Get the document  
  85.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  86.         //Get the first root node  
  87.         Element root = document.getRootElement();  
  88.         //Get the first p1  
  89.         Element p1 = root.element("p1");  
  90.         //Get the age below p1  
  91.         Element age = p1.element("age");  
  92.         //Modify the value of age  
  93.         age.setText("300");  
  94.         //Write back xml  
  95.         Dom4jUtils.xmlWriters(Dom4jUtils.PATH, document);  
  96.     }  
  97.       
  98.     //Add < School > ecit. edu. CN < / schlool > before the age tag under the first p1  
  99.     public static void addAgeBefore() throws Exception {  
  100.         /* 
  101.          * 1,Create parsers 
  102.          * 2,Get the document 
  103.          * 3,Get the root node 
  104.          * 4,Get the first p1 
  105.          *  
  106.          * 5,Get all the elements below p1 
  107.          *      ** elements()Method to return the list collection 
  108.          *      ** Use the method in the list to add elements at specific locations 
  109.          *      ** Create Elements: Create Text under Elements 
  110.          *          *** add(int index, E element) 
  111.          *              - The first parameter is the position subscript, starting at 0. 
  112.          *              - The second parameter is the element to be added 
  113.          * 6,Write back xml 
  114.          * */  
  115.         //Create parsers  
  116. //      SAXReader saxReader = new SAXReader();  
  117.         //Get the document  
  118. //      Document document = saxReader.read("src/p1.xml");  
  119.           
  120.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  121.         //Get the root node  
  122.         Element root = document.getRootElement();  
  123.         //Get the first p1  
  124.         Element p1 = root.element("p1");  
  125.         //Get all the elements below p1  
  126.         List<Element> list = p1.elements();  
  127.         //Create elements using  
  128.         Element school = DocumentHelper.createElement("school");  
  129.         //Create text under school  
  130.         school.setText("ecit");   
  131.         //Adding at a specific location  
  132.         list.add(1, school);  
  133.         //Write back xml  
  134.         /*OutputFormat format = OutputFormat.createPrettyPrint(); 
  135.         XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/p1.xml"), format); 
  136.         xmlWriter.write(document); 
  137.         xmlWriter.close();*/  
  138.         Dom4jUtils.xmlWriters(Dom4jUtils.PATH, document);  
  139.     }  
  140.       
  141.     //Add an element at the end of the first p1 tag <sex>nv</sex>  
  142.     public static void addSex() throws Exception {  
  143.         /* 
  144.          * 1,Create parsers 
  145.          * 2,Get the document 
  146.          * 3,Get the root node 
  147.          *  
  148.          * 4,Get the first p1 
  149.          * 5,Add elements below p1 
  150.          * 6,Add text below the element after the addition is complete 
  151.          *  
  152.          * 7,Write back xml 
  153.          * */  
  154.         //Create parsers  
  155. //      SAXReader reader = new SAXReader();  
  156.         //Get the document  
  157. //      Document document = reader.read("src/p1.xml");  
  158.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  159.         //Get the root node  
  160.         Element root = document.getRootElement();  
  161.         //Get the first p1 element  
  162.         Element p1 = root.element("p1");  
  163.         //Add elements directly below p1  
  164.         Element sex1 = p1.addElement("sex");  
  165.         //Add text under sex  
  166.         sex1.setText("nv");  
  167.           
  168.         //Write back xml  
  169. //OutputFormat format = OutputFormat.createPrettyPrint(); // Can have indentation effect  
  170. ////        OutputFormat format = OutputFormat.createCompactFormat();  
  171. //      XMLWriter  xmlWriter = new XMLWriter(new FileOutputStream("src/p1.xml"), format);  
  172. //      xmlWriter.write(document);  
  173. //      xmlWriter.close();  
  174.         Dom4jUtils.xmlWriters(Dom4jUtils.PATH, document);  
  175.     }  
  176.       
  177.     //Get the value in the second name element  
  178.     public static void selectSecond() throws Exception {      
  179.         /* 
  180.          * 1,Create parsers 
  181.          * 2,Get the document 
  182.          * 3,Get the root node 
  183.          *  
  184.          *  4,Get all p1 
  185.          *  5,Traverse to get the second p1 
  186.          *  6,Get the name below the second p1 
  187.          *  7,Get the value of name 
  188.          * */  
  189.         //Create parsers  
  190.         SAXReader saxReader = new SAXReader();  
  191.         //Get the document  
  192.         Document document = saxReader.read("src/p1.xml");  
  193.         //Get the root node  
  194.         Element root = document.getRootElement();  
  195.         //Get all p1  
  196.         List<Element> list = root.elements("p1");  
  197.         //Get the second p1 list set subscript from 0  
  198.         Element p2 = list.get(1);  
  199.         //Get the name below p1  
  200.         Element name2 = p2.element("name");  
  201.         //Get the value in name  
  202.         String s2 = name2.getText();  
  203.         System.out.println(s2);  
  204.     }  
  205.       
  206.     //Get the value in a name element  
  207.     public static void selectSin() throws Exception {         
  208.         /* 
  209.          * 1,Create parsers 
  210.          * 2,Get the document 
  211.          * 3,Get the root node 
  212.          *  
  213.          * 4,Get the first p1 element 
  214.          * 5,Get the name element below p1 
  215.          * 6,Get the value in the name element 
  216.          * */  
  217.         //Create parsers  
  218.         SAXReader saxReader = new SAXReader();  
  219.         //Get the document  
  220.         Document document = saxReader.read("src/p1.xml");  
  221.         //Get the root node  
  222.         Element root = document.getRootElement();  
  223.         //Get the first p1  
  224.         Element p1 = root.element("p1");  
  225.         //Get the name element below p1  
  226.         Element name1 = p1.element("name");  
  227.         //Get the value of name  
  228.         String s1 = name1.getText();  
  229.         System.out.println(s1);  
  230.     }  
  231.   
  232.     //Query the values of all name elements in xml  
  233.     public static void selectName() throws Exception {  
  234.         /* 
  235.          * 1,Create parsers 
  236.          * 2,Get the document 
  237.          * 3,Get the root node 
  238.          *  
  239.          * 4,Get p1 
  240.          * 5,Get the name below p1 
  241.          * 6,Get the value in name 
  242.          * */  
  243.         //Create parsers  
  244.         SAXReader saxReader = new SAXReader();  
  245.         //Get the document  
  246.         Document document = saxReader.read("src/p1.xml");  
  247.         //Get the root node  
  248.         Element root = document.getRootElement();  
  249.           
  250.         //Get p1  
  251.         List<Element> list = root.elements("p1");  
  252.         //Traversing list  
  253.         for (Element element : list) {  
  254.             //Element is every p1 element  
  255.             //Get the name element below p1  
  256.             Element name1 = element.element("name");  
  257.             //Get the value in name  
  258.             String s = name1.getText();  
  259.             System.out.println(s);  
  260.         }  
  261.     }  
  262. }  

1.2 TestDom4jXpath.java

  1. package cn.itcast.dom4jtest;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.dom4j.Document;  
  6. import org.dom4j.Node;  
  7.   
  8. import cn.itcast.utils.Dom4jUtils;  
  9.   
  10. public class TestDom4jXpath {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      * @throws Exception  
  15.      */  
  16.     public static void main(String[] args) throws Exception {  
  17. //      test1();  
  18.         test2();  
  19.   
  20.     }  
  21.       
  22.     //Implemented with xpath: Get the value of name under the first p1  
  23.     public static void test2() throws Exception {  
  24.         /* 
  25.          * 1,Get the document 
  26.          * 2,Direct implementation using selectSingleNode method 
  27.          *  - xpath :  //p1[@id1='aaaa']/name 
  28.          * */  
  29.         //Get the document  
  30.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  31.         //Direct implementation using selectSingleNode method  
  32.         Node name1 = document.selectSingleNode("//p1[@id1='aaaa']/name"); //The element of name
  33.         //Get the value in name  
  34.         String s1 = name1.getText();  
  35.         System.out.println(s1);  
  36.     }  
  37.       
  38.     //Query the values of all name elements in xml  
  39.     public static void test1() throws Exception {  
  40.         /* 
  41.          * 1,Get the document 
  42.          * 2,Get all name elements directly using the selectNodes("//name") method 
  43.          *  
  44.          * */  
  45.         //Get the document  
  46.         Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);  
  47.         //Get all name elements using the selectNodes("//name") method  
  48.         List<Node> list = document.selectNodes("//name");  
  49.         //Traversing list sets  
  50.         for (Node node : list) {  
  51.             //node is each name element  
  52.             //Get the value in the name element  
  53.             String s = node.getText();  
  54.             System.out.println(s);  
  55.         }  
  56.     }  
  57.   
  58. }  

1.3Dom4jUtils,java

  1. package cn.itcast.utils;  
  2.   
  3. import java.io.FileOutputStream;  
  4.   
  5. import org.dom4j.Document;  
  6. import org.dom4j.DocumentException;  
  7. import org.dom4j.io.OutputFormat;  
  8. import org.dom4j.io.SAXReader;  
  9. import org.dom4j.io.XMLWriter;  
  10.   
  11. public class Dom4jUtils {  
  12.       
  13.     public static final String PATH = "src/p1.xml";  
  14.   
  15.     //Return to document  
  16.     public static Document getDocument(String path) {  
  17.         try {  
  18.             //Create parsers  
  19.             SAXReader reader = new SAXReader();  
  20.             //Get the document  
  21.             Document document = reader.read(path);  
  22.             return document;  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return null;  
  27.     }  
  28.       
  29.     //Writing back xml  
  30.     public static void xmlWriters(String path,Document document) {  
  31.         try {  
  32.             OutputFormat format = OutputFormat.createPrettyPrint();  
  33.             XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path), format);  
  34.             xmlWriter.write(document);  
  35.             xmlWriter.close();  
  36.         }catch(Exception e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40. }  

1.4 p1.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <person>   
  4.   <p1 id1="aaaa">   
  5.     <name>zhangsan</name>    
  6.     <age>300</age>    
  7.     <sex>nv</sex>   
  8.   </p1>    
  9.   <p1>   
  10.     <name>lisi</name>    
  11.     <age>30</age>   
  12.   </p1>   
  13. </person>  



2. Query xml with dom4j

* Parsing is top-down parsing
* Query the values in all name elements
/*
1. Create parsers
2. Obtain document
3. Get the root node getRootElement() back to Element


4. Get all the p1 Tags
* elements("p1") returns the list collection
* Traverse the list to get each p1
5. Get the name
* Execute the element("name") method under p1 to return Element
6. Get the value in name
* getText method gets the value
*/

* Query the value of the first name element
/*
* 1. Create parsers
* 2. Obtain document
* 3. Get the root node

* 4. Get the first p1 element.
** element("p1") method returns Element
* 5. Get the name element under p1
** element("name") method returns Element
* 6. Get the value in the name element
** getText method
* */
* Get the value of the second name element
/*
* 1. Create parsers
* 2. Obtain document
* 3. Get the root node

* 4. Get all p1
** Return to list collection
* 5. Traversing to get the second p1
** Use the list subscript to get the get method. The set subscript starts at 0. To get the second value, subscript 1.
* 6. Get the name below the second p1
** element("name") method returns Element
* 7. Get the value of name
** getText method
* */

3. Use dom4j to implement add operation

* Add an element < sex > NV </sex > at the end of the first p1 tag.
* steps
/*
* 1. Create parsers
* 2. Obtain document
* 3. Get the root node

* 4. Get the first p1
* Use element method
* 5. Add elements under p1
* Return an Element directly on p1 using the addElement("tag name") method


* 6. Add text below the element after adding
* Use the setText("text content") method directly on sex
* 7. Write back xml
* Format OutputFormat and use the createPrettyPrint method to represent a beautiful format
* Using the class XMLWriter directly to the new class, pass two parameters
*** The first parameter is the xml file path new FileOutputStream("path")
*** The second parameter is the value of the formatted class
* */


4. Use dom4j to add elements at specific locations

* Add < School > ecit. edu. CN < / schlool > before the age tag under the first p1
* steps
   /*
* 1. Create parsers
* 2. Obtain document
* 3. Get the root node
* 4. Get the first p1

* 5. Get all the elements under p1
* ** elements() method returns list set


* ** Add elements at specific locations using the method in the list
* ** First create the element to create text under the element
- Create tags using the DocumentHelper class method createElement
- Add text to the label using the setText("text content") method


*       add(int index, E element) in *** list collection
*       - The first parameter is the position subscript, starting at 0.
*       - The second parameter is the element to add.
* 6. Write back xml
* */


** It can encapsulate the operations that get the document and write back xml into methods
** You can also encapsulate the passed file path as a constant
*** Benefits: Increased development speed and submission of code maintainability
- For example, if you want to change the file path (name), you only need to change the value of the constant at this time, and no other code needs to change.


5. Using dom4j to implement the operation of modifying nodes

* Modify the value of age element under the first p1 < age > 30 </age >
* steps
/*
* 1. Get the document
* 2. Get the root node, and then get the first p1 element
* 3. Get the age below the first p1
element("") method
* 4. The modified value is 30
* * Using the setText("text content") method
* 5. Write back xml

* */


6. Use dom4j to delete nodes

* Delete the < School > ECIT </school > element under the first p1
* steps
/*
* 1. Get the document
* 2. Get the root node
* 3. Get the first p1 tag
* 4. Get the school element under the first p1


* 5. Delete (use p1 to delete school)
* * Get the parent node of school
- The first is to get p1 directly.
- Using the method getParent to get
* Delete operation
- Execute remove method on p1 to delete nodes
* 6. Write back xml
* */


7. Use dom4j to get attributes

* Get the value of attribute id1 in the first p1
* steps
/*
* 1. Get the document
* 2. Get the root node
* 3. Get the first p1 element
* 4. Get the attribute values in p1
- p1.attributeValue("id1");
- Execute this method on p1, where the parameter is the property name
* */


8. Supporting xpath operations with dom4j

* You can get an element directly.


* The first form
/ AAA/DDD/BBB: A layer by layer, a BBB under DDD under AAA
* The second form
// BBB: Represents the same name as BBB. It means that as long as the name is BBB, you get it.
* The third form
/* All elements
* The fourth form
** BBB[1]: Represents the first BBB element
* BBB[last()]: Represents the last BBB element
* The fifth form
**//BBB[@id]: Indicates that as long as the BBB element has an ID attribute on it, it gets
* Sixth form
**//BBB[@id='b1'] denotes that the element name is BBB, that there is an ID attribute on BBB, and that the attribute value of ID is b1.


9. Supporting xpath operations with dom4j

** dom4j does not support xpath by default
** If you want to have xpath in dom4j
* The first step is to introduce a jar package that supports xpath, using jaxen-1.1-beta-6.jar
** The jar package needs to be imported into the project


** Two methods are provided in dom4j to support xpath
*** selectNodes("xpath expression")
- Getting multiple nodes
*** selectSingleNode("xpath expression")
- Get a node


** Implemented with xpath: Query the values of all name elements in xml
** The xpath representation of all name elements: //name
** Use selectNodes("//name");
** Code and steps
/*
* 1. Get the document
* 2. Get all name elements directly using the selectNodes("//name") method

* */
// Get the document
Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);
// Get all name elements using the selectNodes("//name") method
List<Node> list = document.selectNodes("//name");
// Traversing list sets
for (Node node : list) {
// node is each name element
// Get the value in the name element
String s = node.getText();
System.out.println(s);
}

** Implemented using xpath: Get the value of name under the first p1
* //p1[@id1='aaaa']/name
* Use selectSingleNode("//p1[@id1='aaaa']/name")
* Steps and code
/*
* 1. Get the document
* 2. Implement directly by using selectSingleNode method
* - xpath : //p1[@id1='aaaa']/name
* */
// Get the document
Document document = Dom4jUtils.getDocument(Dom4jUtils.PATH);
// Direct implementation using selectSingleNode method
Node name 1 = document. selectSingleNode ("//p1[@id1='aaaa']/name"); element of //name)
// Get the value in name
String s1 = name1.getText();
System.out.println(s1);

Topics: xml Attribute Java encoding