Search code examples
javajsonxmlxpathpojo

XML schema to Java pojos to json


I'm generating a Java class hierarchy from a complicated xml schema. The content stored in xml (returned via a REST endpoint) is verbose, plus consumers want only subsets of the xml content returned as json (that they define).

I know there are brute force ways to accomplish this, but does anyone know of a more elegant approach? Maybe some kind of mapping that the consumer could pass to the service that would allow them to dynamically select the content (via xpath in the mapping) and also produce custom json wrapping that content?

The consumers will have an understanding of the schema and content structure, but nothing about Java or its object hierarchy.

I know, a lot here. Any suggestions?


Solution

  • You can give Apache Commons JXPath a try. If you have the Java class hierarchy then I assume you have the objects in memory before sending them as XML. With that you can user JXPath to traverse the java objects as if they were XML in XPath fashion.