How to Use XPath on JAXB Content Trees
Java's XPath engine is not able to use JAXB content trees directly. You either need to convert it to a DOM tree or to a binary XML document first. The following example shows you how to use DOM for running XPath:
MovieLibrary library = ...;
JAXBContext ctx = JAXBContext.newInstance(MovieLibrary.class);
Marshaller marshaller = ctx.createMarshaller();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document doc = domBuilder.newDocument();
marshaller.marshal(library, doc);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
NodeList list = (NodeList) xpath.evaluate(
"/movieLibrary/collection[releaseYear < 1980]/title",
doc, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) // print list of movies older than 1980
System.out.println(list.item(i).getTextContent());
An alternative for running 'real' XPath expressions over the XML is to use JXPath, which allows you to run XPath expressions over Java object trees.

