<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
//language=XML
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root xmlns:my=\"ns-telo\">\n" +
" <tag>first</tag>\n" +
" <tag type=\"special\">second</tag>\n" +
" <other>third</other>\n" +
" <other type=\"\">fourth</other>\n" +
" <!-- XPath -->\n" +
" <deep>\n" +
" <tag>\n" +
" <other>fifth</other>\n" +
" <other id=\"att-telo\">value att telo</other>\n" +
" </tag>\n" +
" </deep>\n" +
" <!-- namespace -->\n" +
" <my:other>sixth</my:other>\n" +
" <my:other my:type=\"different\">seventh</my:other>\n" +
" <empty></empty>\n" +
"</root>";
SAXBuilder saxBuilder = new SAXBuilder();
Document document = null;
try {
document = saxBuilder.build(IOUtils.toInputStream(s));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = document.getRootElement();
// situation 1
// searching for the tag-Node
for (Element node : root.getChildren()) {
if (node.getName().equals("tag")) {
System.out.println(node.getName());
}
}
// iterating for the tag-Node
for (Element node : root.getChildren("tag")) {
System.out.println(node.getName());
}
// searching for the other-Node with attribute type
for (Element node : root.getChildren("other")) {
if (node.getAttribute("type") != null) {
System.out.println(node.getAttribute("type").getValue());
}
}
// iterating over all nodes
for (Content content : root.getDescendants()) {
System.out.println(content);
}
// situation 2
// namespaces
Namespace nsMy = root.getNamespace("my");
System.out.println("nsMy: " + nsMy.getPrefix() + " | " + nsMy.getURI());
System.out.println("other: " + root.getChild("other", nsMy).getTextTrim());
for (Element node : root.getChildren("other", nsMy)) {
if (node.getAttribute("type", nsMy) != null) {
System.out.println(node.getAttribute("type", nsMy).getValue());
}
}
// Xpath get Content
XPathFactory xpathFactory = XPathFactory.instance();
XPathExpression<Object> expr = xpathFactory.compile("root/deep/tag/other/text()");
List<Object> xPathSearchedNodes = expr.evaluate(document);
for (int i = 0; i < xPathSearchedNodes.size(); i++) {
Content content = (Content) xPathSearchedNodes.get(i);
System.out.println(content.getValue());
}
// Xpath get Attribute
expr = xpathFactory.compile("root/deep/tag/other/@id");
xPathSearchedNodes = expr.evaluate(document);
for (int i = 0; i < xPathSearchedNodes.size(); i++) {
Attribute attribute = (Attribute) xPathSearchedNodes.get(i);
System.out.println(attribute.getValue());
}
// Xpath get value by Attribute
expr = xpathFactory.compile("root/deep/tag/other[@id='att-telo']");
xPathSearchedNodes = expr.evaluate(document);
for (int i = 0; i < xPathSearchedNodes.size(); i++) {
Content content = (Content) xPathSearchedNodes.get(i);
System.out.println(content.getValue());
}