forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPathLesson.java
More file actions
34 lines (29 loc) · 1.6 KB
/
Copy pathXPathLesson.java
File metadata and controls
34 lines (29 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package xml;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
public class XPathLesson {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("prop.xml"));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
System.out.println(xpath.evaluate("/preferences/root/node/node/node/map", document));
System.out.println(xpath.evaluate("/preferences/root/node/node/node/map[2]", document));
System.out.println(xpath.evaluate("count(/preferences/root/node/node/node/map)", document));
NodeList list = (NodeList) xpath.evaluate("/preferences/root/node/node/node/map", document, XPathConstants.NODESET);
Node node = (Node) xpath.evaluate("/preferences/root/node/node/node/map[2]", document, XPathConstants.NODE);
int count = ((Number)xpath.evaluate("count(/preferences/root/node/node/node/map)", document, XPathConstants.NUMBER)).intValue();
}
}