forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaXPathEx.java
More file actions
108 lines (74 loc) · 3.63 KB
/
JavaXPathEx.java
File metadata and controls
108 lines (74 loc) · 3.63 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.zetcode;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
public class JavaXPathEx {
public static void main(String[] args) throws ParserConfigurationException,
IOException, SAXException, XPathExpressionException {
var factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // required
var builder = factory.newDocumentBuilder();
var doc = builder.parse("src/main/resources/inventory.xml");
var xPathFactory = XPathFactory.newInstance();
var xpath = xPathFactory.newXPath();
System.out.println("Book titles written in 2018");
var expr = xpath.compile("//book[@year=2018]/title/text()");
var result = expr.evaluate(doc, XPathConstants.NODESET);
var nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
System.out.println("\nBook titles cheaper than 40");
expr = xpath.compile("//book[price<40]/title/text()");
result = expr.evaluate(doc, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
System.out.println("\nReading the comment");
expr = xpath.compile("//inventory/comment()");
result = expr.evaluate(doc, XPathConstants.STRING);
var comment = (String) result;
System.out.println(comment.trim());
System.out.println("\nCount all book titles ");
expr = xpath.compile("count(//book/title)");
result = expr.evaluate(doc, XPathConstants.NUMBER);
var nOfTitles = (Double) result;
System.out.printf("There are %d book titles%n", nOfTitles.intValue());
System.out.println("\nCount all publishers");
// XSLT 2.0 has better syntax for unique
expr = xpath.compile("count(//book/publisher[not(following::book/publisher/text() = text())])");
result = expr.evaluate(doc, XPathConstants.NUMBER);
var nOfPublishers = (Double) result;
System.out.printf("There are %d publishers%n", nOfPublishers.intValue());
System.out.println("\nList all authors (possible duplicates)");
expr = xpath.compile("//book/author/text()");
result = expr.evaluate(doc, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
System.out.println("\nFound book titles containing Python");
expr = xpath.compile("//book[contains(title, 'Python')]");
result = expr.evaluate(doc, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i)
.getChildNodes()
.item(1) //node <title> is on first index
.getTextContent());
}
System.out.println("\nGet book title added in last node");
expr = xpath.compile("//book[last()]/title/text()");
result = expr.evaluate(doc, XPathConstants.NODESET);
nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}