Skip to content

Commit ae3f45e

Browse files
2_09_XPath
1 parent 51fef92 commit ae3f45e

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import org.w3c.dom.Document;
4+
import org.xml.sax.SAXException;
5+
6+
import javax.xml.namespace.QName;
7+
import javax.xml.parsers.DocumentBuilder;
8+
import javax.xml.parsers.DocumentBuilderFactory;
9+
import javax.xml.parsers.ParserConfigurationException;
10+
import javax.xml.xpath.XPath;
11+
import javax.xml.xpath.XPathExpression;
12+
import javax.xml.xpath.XPathExpressionException;
13+
import javax.xml.xpath.XPathFactory;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
17+
public class XPathProcessor {
18+
private static final DocumentBuilderFactory DOCUMENT_FACTORY = DocumentBuilderFactory.newInstance();
19+
private static final DocumentBuilder DOCUMENT_BUILDER;
20+
21+
private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance();
22+
private static final XPath XPATH = XPATH_FACTORY.newXPath();
23+
24+
static {
25+
DOCUMENT_FACTORY.setNamespaceAware(true);
26+
try {
27+
DOCUMENT_BUILDER = DOCUMENT_FACTORY.newDocumentBuilder();
28+
} catch (ParserConfigurationException e) {
29+
throw new IllegalStateException(e);
30+
}
31+
}
32+
33+
private final Document doc;
34+
35+
public XPathProcessor(InputStream is) {
36+
try {
37+
doc = DOCUMENT_BUILDER.parse(is);
38+
} catch (SAXException | IOException e) {
39+
throw new IllegalArgumentException(e);
40+
}
41+
}
42+
43+
public static synchronized XPathExpression getExpression(String exp) {
44+
try {
45+
return XPATH.compile(exp);
46+
} catch (XPathExpressionException e) {
47+
throw new IllegalArgumentException(e);
48+
}
49+
}
50+
51+
public <T> T evaluate(XPathExpression expression, QName type) {
52+
try {
53+
return (T) expression.evaluate(doc, type);
54+
} catch (XPathExpressionException e) {
55+
throw new IllegalArgumentException(e);
56+
}
57+
}
58+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.javaops.masterjava.xml.util;
2+
3+
import com.google.common.io.Resources;
4+
import org.junit.Test;
5+
import org.w3c.dom.NodeList;
6+
7+
import javax.xml.xpath.XPathConstants;
8+
import javax.xml.xpath.XPathExpression;
9+
import java.io.InputStream;
10+
import java.util.stream.IntStream;
11+
12+
public class XPathProcessorTest {
13+
@Test
14+
public void getCities() throws Exception {
15+
try (InputStream is =
16+
Resources.getResource("payload.xml").openStream()) {
17+
XPathProcessor processor = new XPathProcessor(is);
18+
XPathExpression expression =
19+
XPathProcessor.getExpression("/*[name()='Payload']/*[name()='Cities']/*[name()='City']/text()");
20+
NodeList nodes = processor.evaluate(expression, XPathConstants.NODESET);
21+
IntStream.range(0, nodes.getLength()).forEach(
22+
i -> System.out.println(nodes.item(i).getNodeValue())
23+
);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)