00 05Java Web development of XPATH

1 XPATH Introduction

1, can be directly to an element
2, a first form of writing: /AAA/DDD/BBB: represents a layer by layer, DDD following below AAA BBB
. 3, a second written forms: //BBB(Select all elements BBB), /DDD//BBB(selection All the BBB DDD parent element of the element)
4, a third form of writing: /AAA/BBB/DDD*(select all paths attached / AAA / BBB / element of DDD), /*/*/*/BBB(BBB select all elements are ancestor elements 3), //*(select all elements ).
5, the fourth written forms: /AAA/BBB[1](Select the first element in the AAA BBB element) /AAA/BBB[last()](select the last element in the AAA BBB element)
6, fifth written forms: //@id(Select all id attribute), //BBB[@id](selection element has an id attribute BBB)
7, sixth written forms: //BBB[@id="b1"](id attribute is selected, the value of the element b1 is BBB)

2 Use dom4j XPATH support operations

By default, dom4j xpath does not support, if you want to use the inside dom4j xpath, the following steps:
(1) introduction supporting xpath jar package: jaxen-1.2.0.
(2) The project package into jar.

In dom4j which provides two methods modified to support XPath
| --selectNodes ( "XPath expression The"), obtaining a plurality of nodes
| --selectSingleNode ( "xpath expression") , obtaining a single node

Query the value of the name element of the xml.
| - //name, xpath all name elements of representation.
(1) to give Document
(2) directly obtained all name elements xpath

public static void getName() {
		Document document = getDocument();
		List<Node> list = document.selectNodes("//name");
		for(Node node : list) {
			System.out.println(node.getText());
		}
	}

3 Use dom4j support the operation of XPATH

Obtaining a first value p1 below the name.

public static void getName() {
		Document document = getDocument();
		Node name1 = document.selectSingleNode("//p1[@id1='love']/name");
		System.out.println(name1.getText());
	}
Published 77 original articles · won praise 11 · views 2628

Guess you like

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