Skip to content

Commit a156fa8

Browse files
committed
Introduction to XPath
1 parent 9be037e commit a156fa8

6 files changed

Lines changed: 480 additions & 1 deletion

File tree

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<module>spring-thymeleaf</module>
6767
<module>spring-zuul</module>
6868
<module>jsf</module>
69-
69+
<module>xml</module>
7070
</modules>
7171

7272
</project>

‎xml/pom.xml‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>xml</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
8+
<name>xml</name>
9+
10+
<dependencies>
11+
<!-- utils -->
12+
13+
<dependency>
14+
<groupId>com.google.guava</groupId>
15+
<artifactId>guava</artifactId>
16+
<version>${guava.version}</version>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>commons-io</groupId>
21+
<artifactId>commons-io</artifactId>
22+
<version>2.4</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.apache.commons</groupId>
27+
<artifactId>commons-collections4</artifactId>
28+
<version>4.0</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.apache.commons</groupId>
33+
<artifactId>commons-lang3</artifactId>
34+
<version>${commons-lang3.version}</version>
35+
</dependency>
36+
37+
38+
<!-- test scoped -->
39+
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>${junit.version}</version>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.hamcrest</groupId>
49+
<artifactId>hamcrest-core</artifactId>
50+
<version>${org.hamcrest.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.hamcrest</groupId>
55+
<artifactId>hamcrest-library</artifactId>
56+
<version>${org.hamcrest.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>org.mockito</groupId>
62+
<artifactId>mockito-core</artifactId>
63+
<version>${mockito.version}</version>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
</dependencies>
68+
69+
<build>
70+
<finalName>xml</finalName>
71+
<resources>
72+
<resource>
73+
<directory>src/main/resources</directory>
74+
<filtering>true</filtering>
75+
</resource>
76+
</resources>
77+
78+
<plugins>
79+
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-compiler-plugin</artifactId>
83+
<version>${maven-compiler-plugin.version}</version>
84+
<configuration>
85+
<source>1.8</source>
86+
<target>1.8</target>
87+
</configuration>
88+
</plugin>
89+
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-surefire-plugin</artifactId>
93+
<version>${maven-surefire-plugin.version}</version>
94+
</plugin>
95+
96+
</plugins>
97+
98+
</build>
99+
100+
<properties>
101+
<!-- persistence -->
102+
<hibernate.version>4.3.11.Final</hibernate.version>
103+
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
104+
105+
<!-- marshalling -->
106+
<jackson.version>2.7.2</jackson.version>
107+
108+
<!-- logging -->
109+
<org.slf4j.version>1.7.13</org.slf4j.version>
110+
<logback.version>1.1.3</logback.version>
111+
112+
<!-- various -->
113+
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
114+
115+
<!-- util -->
116+
<guava.version>19.0</guava.version>
117+
<commons-lang3.version>3.4</commons-lang3.version>
118+
119+
<!-- testing -->
120+
<org.hamcrest.version>1.3</org.hamcrest.version>
121+
<junit.version>4.12</junit.version>
122+
<mockito.version>1.10.19</mockito.version>
123+
124+
<httpcore.version>4.4.1</httpcore.version>
125+
<httpclient.version>4.5</httpclient.version>
126+
127+
<rest-assured.version>2.9.0</rest-assured.version>
128+
129+
<!-- maven plugins -->
130+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
131+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
132+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
133+
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
134+
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
135+
136+
</properties>
137+
138+
</project>
139+
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package com.baeldung.xml;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.Iterator;
7+
8+
import javax.xml.namespace.NamespaceContext;
9+
import javax.xml.parsers.DocumentBuilder;
10+
import javax.xml.parsers.DocumentBuilderFactory;
11+
import javax.xml.parsers.ParserConfigurationException;
12+
import javax.xml.xpath.XPath;
13+
import javax.xml.xpath.XPathConstants;
14+
import javax.xml.xpath.XPathExpressionException;
15+
import javax.xml.xpath.XPathFactory;
16+
17+
import org.w3c.dom.Document;
18+
import org.w3c.dom.Node;
19+
import org.w3c.dom.NodeList;
20+
import org.xml.sax.SAXException;
21+
22+
public class DefaultParser {
23+
24+
private File file;
25+
26+
public DefaultParser(File file) {
27+
this.file = file;
28+
}
29+
30+
public NodeList getFirstLevelNodeList() {
31+
NodeList nodeList = null;
32+
try {
33+
FileInputStream fileIS = new FileInputStream(this.getFile());
34+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
35+
36+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
37+
38+
Document xmlDocument = builder.parse(fileIS);
39+
40+
XPath xPath = XPathFactory.newInstance().newXPath();
41+
42+
String expression = "/Tutorials/Tutorial";
43+
44+
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
45+
46+
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
47+
e.printStackTrace();
48+
}
49+
return nodeList;
50+
}
51+
52+
public Node getNodeById(String id) {
53+
Node node = null;
54+
try {
55+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
56+
57+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
58+
59+
Document xmlDocument = builder.parse(this.getFile());
60+
61+
XPath xPath = XPathFactory.newInstance().newXPath();
62+
63+
String expression = "/Tutorials/Tutorial[@tutId=" + "'" + id + "'" + "]";
64+
65+
node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
66+
67+
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
68+
e.printStackTrace();
69+
}
70+
return node;
71+
}
72+
73+
public NodeList getNodeListByTitle(String name) {
74+
NodeList nodeList = null;
75+
try {
76+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
77+
78+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
79+
80+
Document xmlDocument = builder.parse(this.getFile());
81+
82+
this.clean(xmlDocument);
83+
84+
XPath xPath = XPathFactory.newInstance().newXPath();
85+
86+
String expression = "//Tutorial[descendant::title[text()=" + "'" + name + "'" + "]]";
87+
88+
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
89+
90+
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
91+
e.printStackTrace();
92+
}
93+
return nodeList;
94+
}
95+
96+
public NodeList getElementsByDate(String date) {
97+
NodeList nodeList = null;
98+
99+
try {
100+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
101+
102+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
103+
104+
Document xmlDocument = builder.parse(this.getFile());
105+
106+
this.clean(xmlDocument);
107+
108+
XPath xPath = XPathFactory.newInstance().newXPath();
109+
110+
String expression = "//Tutorial[number(translate(date, '/', '')) > " + date + "]";
111+
112+
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
113+
114+
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
115+
e.printStackTrace();
116+
}
117+
return nodeList;
118+
}
119+
120+
public NodeList getAllTutorials() {
121+
NodeList nodeList = null;
122+
try {
123+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
124+
builderFactory.setNamespaceAware(true);
125+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
126+
127+
Document xmlDocument = builder.parse(this.getFile());
128+
129+
this.clean(xmlDocument);
130+
131+
XPath xPath = XPathFactory.newInstance().newXPath();
132+
133+
xPath.setNamespaceContext(new NamespaceContext() {
134+
135+
@Override
136+
public Iterator getPrefixes(String arg0) {
137+
return null;
138+
}
139+
140+
@Override
141+
public String getPrefix(String arg0) {
142+
return null;
143+
}
144+
145+
@Override
146+
public String getNamespaceURI(String arg0) {
147+
if ("bdn".equals(arg0)) {
148+
return "http://www.baeldung.com/full_archive";
149+
}
150+
return null;
151+
}
152+
});
153+
154+
String expression = "/bdn:Tutorials/bdn:Tutorial";
155+
156+
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
157+
158+
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
159+
e.printStackTrace();
160+
}
161+
return nodeList;
162+
}
163+
164+
private void clean(Node node) {
165+
166+
NodeList childs = node.getChildNodes();
167+
168+
for (int n = childs.getLength() - 1; n >= 0; n--) {
169+
Node child = childs.item(n);
170+
short nodeType = child.getNodeType();
171+
172+
if (nodeType == Node.ELEMENT_NODE)
173+
clean(child);
174+
else if (nodeType == Node.TEXT_NODE) {
175+
String trimmedNodeVal = child.getNodeValue().trim();
176+
if (trimmedNodeVal.length() == 0)
177+
node.removeChild(child);
178+
else
179+
child.setNodeValue(trimmedNodeVal);
180+
} else if (nodeType == Node.COMMENT_NODE)
181+
node.removeChild(child);
182+
}
183+
}
184+
185+
public File getFile() {
186+
return file;
187+
}
188+
189+
public void setFile(File file) {
190+
this.file = file;
191+
}
192+
193+
}

0 commit comments

Comments
 (0)