- Jaxen is an open source X-Path Library.
- It can work with DOM, XOM, dom4j, and JDOM.
- Jaxen and Dom4j library download Link:-
Jaxen lib download link Dom4j lib download link
Input Xml File:-
India
rice
curry
roti
tandoori
coconut water
lemon water
pulao
barfi
palak paneer
sahi kabab
US
pizza
pepsi
coke
red bull
hot dog
smoky chicken
Project Structure:-
Test Jaxen xpath:-
package com.sandeep.jaxen.xpath;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jaxen.JaxenException;
import org.jaxen.XPath;
import org.jaxen.dom4j.Dom4jXPath;
public class JaxenXPathTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
File xmlFile = new File("input-xml-file.xml");
SAXReader reader = new SAXReader();
Document dom4jDocument = null;
XPath path = null;
Listresults = null;
try {
dom4jDocument = reader.read(xmlFile);
path = new Dom4jXPath("Countries/Country/Name");
results = path.selectNodes(dom4jDocument);
for (Element element : results) {
System.out.println(element.getData());
}
} catch (JaxenException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}