views:

54

answers:

2

I'm looking for a Java library that would allow me to marshal XML to a Java object tree, and vice versa. There are plenty of libraries that would allow me to bind XML to JavaBeans generated by some code generation tool, however, I don't need those (JAXB, JiBX, Castor and so on).

What I need is a tool which would consume a schema file and an xml file and then return a combination of Maps, Lists and Objects in a manner similar to Jackson's simple data binding (when it is possible, of course). Jackson is intended for JSON, not for XML; and it lacks the ability to take a schema file in account (because JSON Schema is too immature at the moment).

Can I adapt some existing tools to solve my problem, or should I roll out my own solution with DOM and XSOM?

A: 

Looks like SOAP. An option is Apache Axis (we use it a lot), but there are other implementations.

G B
+1  A: 

MOXy's Dynamic JAXB

MOXy offers a dynamic JAXB implementation. You can bootstrap from an XML schema and instead of static classes you can interact with instances of DynamicEntity with generic get/set methods:

FileInputStream xsd = new FileInputStream("src/example/customer.xsd");
DynamicJAXBContext jaxbContext = 
    DynamicJAXBContextFactory.createContextFromXSD(xsd, null, null, null);

FileInputStream xmlInputStream = new FileInputStream("src/example/dynamic/customer.xml");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);

System.out.println(customer.<String>get("name"));

For more information see:

Service Data Objects (SDO)

You could also use Service Data Objects for this (JSR-235).

FileReader xsd = new FileReader("customer.xsd");
XSDHelper.INSTANCE.define(xsd, null);

FileReader xml = new FileReader("input.xml");
XMLDocument doc = XMLHelper.INSTANCE.load(xml, null, null);

DataObject customerDO = doc.getRootObject();
int id = customerDO.getInt("id");
DataObject addressDO = customerDO.getDataObject("contact-info/address");

For more information see:

Blaise Doughan
DynamicEntities and SDO look like something that would do the job. Thank you!
kohomologie
EclipseLink includes the MOXy component and is the SDO reference implementation so it's a good place to start http://www.eclipse.org/eclipselink/
Blaise Doughan
For Dynamic JAXB I would recommend getting the latest 2.1.1 build http://www.eclipse.org/eclipselink/downloads/nightly.php. It has some bug fixes related to list properties.
Blaise Doughan