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.2 TestDom4jXpath.java
1.3Dom4jUtils,java
1.4 p1.xml
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);