00 04Java XML Web development of the analytical dom4j

Introduction of 1 dom4j

dom4j is an organization for xml parsing, providing the parser dom4j.

dom4j not javase part of, you want to use:
The first step you need to import dom4j provided jar package

Get Document
public class SAXReader extends java.lang.Object

SAXReader reader = new SAXReader();
Document document = reader.read(url);

Document interface is the parent Node
| - If you do not want in the Document method which, to the Node find inside.

method description
public Element getRootElement () Gets the root node, return Element

Element is an interface, the interface is the parent Node

method description
public Element getParent() Get the parent node
public Element addElement add tag
public Element element(qName) Retrieves the label following the first sub-label, qName refers to the name tag
public List elements(qName) Retrieves all below child of the tag (one), qName refers to the name tag
public List elements() It retrieves all of the following sub-label tags (one)

2 Use dom4j for query xml operation

<?xml version="1.0" encoding="UTF-8"?>

<person> 
  <p1 id1="love"> 
    <name>lks</name>  
    <age>30</age>  
  </p1>  
  <p1> 
    <name>hhy</name>  
    <age>20</age> 
  </p1> 
</person>

Query the value of all the name elements inside.
(1) create the parser.
(2) to give a the Document
(. 3) to give the root node
(4) tags get all of the p1: Use elements (qName) method returns a list of all the collection, then traverse a list to give each of p1.
(5) to give name: performing element (qName) method at p1, returns the Element
(. 6) obtained inside of the value of the name: getText () method to obtain the value

public static void allName() throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/dom4jlearn/person.xml");
		Element root = document.getRootElement();
		List<Element> p1 = root.elements("p1");
		for(Element e : p1) {
			Element name = e.element("name");
			String str = name.getText();
			System.out.println(str);
		}
	}

The first query the value of the name element.
(1) create the parser.
(2) to give a the Document
(. 3) to give the root node
(4) to give p1 a first element
(5) to give the following p1 name element
(6) to give the value of the name element inside

public static void getFirstName() throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/dom4jlearn/person.xml");
		Element root = document.getRootElement();
		Element p1 = root.element("p1");
		Element name = p1.element("name");
		System.out.println(name.getText());
	}

The second query value of the name element.
(1) create the parser.
(2) to give a the Document
(. 3) to give the root node
(4) give all the p1 element
(5) to give a second traverse p1
(. 6) to give the following second p1 name element
(7) to give the value of the name element inside

public static void getSecondName()throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/dom4jlearn/person.xml");
		Element root = document.getRootElement();
		List<Element> p1 = root.elements("p1");
		Element name = p1.get(1).element("name");
		System.out.println(name.getText());
	}

3 Use dom4j add nodes to achieve in the end

Add an element to the end of the first p1 label <sex>女</sex>.
(1) create the parser.
(2) to give a the Document
(. 3) to give the root node
(4) to obtain a first p1: Use element () method
(5) p1 additive element in the following: Use p1.addElement ( "sex") method returns an Element
(6) after completion of the element to add the following text: use setText ( "text") method on Sex
(7) back to write xml: format (OutputFormat), using createPrettyPrint (), use XMLWriter, direct new, biography two parameters, the first parameter is xml path, the second parameter is the value of the format type. After using write () write-back

public static void addSex() throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/dom4jlearn/person.xml");
		Element root = document.getRootElement();
		Element p1 = root.element("p1");
		Element sex = p1.addElement("sex");
		sex.setText("女");
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/dom4jlearn/person.xml"), format);
		xmlWriter.write(document);
	}
	

4 dom4j implemented using additional element in a specific position

Add before the first p1 below age label <school>fjut</school>
(1) to create a parser.
(2) to give a the Document
(. 3) to give the root node
(4) acquisition of the first P1
(. 5) Get all the elements p1: elements () method returns a collection of List
A method (6) List inside add (int index, E element), add in a specific location element
| - create elements using DocumentHelper.createElement (), create text below.
(7) write-back xml


```java
public static void addSchool() throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(PATH);
		Element root = document.getRootElement();
		Element p1 = root.element("p1");
		List<Element> list = p1.elements();
		Element school = DocumentHelper.createElement("school");
		school.setText("fjut");
		list.add(1, school);
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(PATH), format);
		xmlWriter.write(document);
	}

# 5 dom4j里面封装方法的操作
可以对得到document的操作和回写xml操作,封装成方法,也可以把传递的文件路径,封装成一个常量,好处是可以提高开发速度,可以提高代码可维护性。 
1、返回document:public static Document getDocument(String path)
2、回写xml:public static void xmlWriters(String path, Document document);

```java
public final static String PATH = "src/dom4jlearn/person.xml";
	
	public static Document getDocument() {
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(PATH);
			return document;
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static void xmlWriters(Document document) {
		
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(PATH), format);
			xmlWriter.write(document);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

6 using the modified node operation implemented dom4j

Modify the first value p1 age following elements <age>30</age>
(1) to give Document
(2) to give p1 a first element
(3) to obtain a first age below p1
(. 4) to modify the value of 30
(5) writeback xml

public static void modifyAge() {
		Document document = Dom4jUtils.getDocument();
		
		Element root = document.getRootElement();
		Element p1 = root.element("p1");
		Element age = p1.element("age");
		age.setText("300");
		
		Dom4jUtils.xmlWriters(document);
	}

7 Use dom4j realize delete node operation

Remove p1 is under a <school>fjut</school>
(1) to give Document
(2) obtaining the root node
(3) obtaining a first label p1
(4) obtained in the first school element p1
(. 5) using the delete p1 school element
(6) xml write back

public static void removeSchool() {
		Document document = getDocument();
		
		Element root = document.getRootElement();
		Element p1 = root.element("p1");
		Element school = p1.element("school");
		p1.remove(school);
		
		xmlWriters(document);
	}

8 dom4j implemented using attribute values ​​of the acquired operation

Obtaining a first value of the attribute id1 p1 inside.
(1) to give Document
(2) obtaining the root node
(3) obtaining a first p1 element
(4) getting a property value of the first element p1, public String attributeValue (String attribute) ;

public static void getAttribute() {
		Document document = getDocument();
		
		Element root = document.getRootElement();
		Element p1 = root.element("p1");
		System.out.println(p1.attributeValue("id1"));
	}
Published 77 original articles · won praise 11 · views 2629

Guess you like

Origin blog.csdn.net/weixin_43762330/article/details/104533554