Short answer: Yes, there is such a solution.
It is called "XML data binding", or alternatively "O/X Mapping" (Object/XML Mapping), or "OXM".
Converting an XML document to an object is called unmarshalling.
Converting (serializing) an object to an XML document is called marshalling.
NOTE:
The terms marshalling and unmarshalling relate NOT ONLY to Object/XML and vice versa transformation. Read here: Marshalling (Computer Science).
Java's own solution is called Java Architecture for XML Binding (JAXB). It is a specification described by JSR 222. JDK includes a JAXB implementation, so you (usually) don't need to download standalone Reference Implementation (RI) from the JAXB Project home page.
NOTE:
You can check, which version of JAXB your JDK has by using xjc (binding compiler), bundled with JDK: xjc -version
Useful links
- Java Architecture for XML Binding - the official Oracle Java learning trail
- A JAXB Tutorial - the official tutorial from the JAXB project (Reference Implementation)
- JAXB hello world example - very simple and easy tutorial
- JAXB tutorial – Getting Started - another short tutorial with examples
Just google "JAXB tutorial", there are tons of them.
Important note:
JAXB is a specification and there are different implementations of it (including Reference Implementation). But these traditional implementations cannot use XPath, which is sad, because for the heavily nested XML files using XPath can be much more effective.
EclipseLink MOXy provides a JAXB implementation with many extensions. One of them is XPath based mapping. I found it extremely useful, when doing one of my projects, where OXM was involved.
Here are some related links:
- XPath Based Mapping - very helpful article from Blaise Doughan, the Team Lead for EclipseLink JAXB (MOXy) project. Also check out other articles in his blog.
- Make your JAXB cleaner with the MOXy implementation - another helpful article with a nice example, outlining the advantage of EclipseLink MOXy.