Java EE development Chapter IX: XML parsing

Introduction: Introduction dom4j parse xml and xpath parse xml.

-------- analytical methods ---------

1.sax: Features: Progressive resolution, only query.

2.dom: Features: a disposable loading the document into the content, may form a tree dom dom tree curd operation.

------- analysis technology ---------

JAXP: sun offers support for DOM and SAX Development Kit

JDom: dom4j brothers

jsoup: processing a particular HTML parsing Development Kit

★ dom4j: commonly used analytical development kits, hibernate underlying employed.

----- dom4j parse xml -------

Use steps: introduction jar package


2. Create a core object SAXReader, new SAXReader ();

3. xml document loaded into memory to form a tree, Document doc = reader.read (file)

4. Get the root node, Element root = doc.getRootElement ();

The other nodes can get (text nodes, attribute nodes, element node) root by
acquiring all of the sub-elements: List <Element> list = root.elements ()
Gets the content element specified attribute: String value = root.attributeValue ( "property name");
obtaining child of the tag body: traversing the list to get every child element, String text = ele.elementText ( "sub-tab name")

6. Code implementation:

Need to parse the contents shown:


import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dom4jDemo {
	public static void main(String[] args) throws Exception {
		//创建核心对象
		SAXReader reader = new SAXReader();
		//获取dom树
		Document doc = reader.read("D:\\fly\\javaee\\Day0801\\xml\\web.xml");
		//获取根节点
		Element root = doc.getRootElement();
		//获取其他节点
		List<Element> list = root.elements();
		//遍历集合
		for (Element ele : list) {
			//获取servlet-name的标签体
			String text = ele.elementText("servlet-name");
//			System.out.println(text);
			String text2 = ele.elementText("url-pattern");
//			System.out.println(text2);
		}
		
		//获取root的version属性值
		String value = root.attributeValue("version");
		System.out.println(value);
	}
}
------- xpath parse xml ------

1, depending on dom4j

2, using the steps of:

1) into the jar package (and the dom4j jaxen-1.1-beta-6.jar)

2) load xml file into memory

3) Use api: selectNode ( "expression"), selectSingleNode ( "expression");

3, written expression:
/ select from the root 
// current node selected to match the selected node in the document, regardless of their location from 
, for example, have an id attribute value of the next label and has 2 = id;
// element name [@ attribute name = "attribute value ']
// element name [@ id =' 2 ']

4, the code:

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XpathDemo {
	public static void main(String[] args) throws Exception {
		Document doc = new SAXReader().read("D:\\fly\\javaee\\Day0801\\xml\\web.xml");
		Element ele = (Element) doc.selectSingleNode("//servlet/servlet-name");
		System.out.println(ele.getText());
	}
}
-------Finish--------

Published 105 original articles · won praise 74 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_32306361/article/details/78072840