Rectangle 27 0

java Define XML Structure with Xstream?

<?xml version="1.0" encoding="UTF-8"?>
List<AnotherRecord> list;
package forum11970410;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11970410/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);


Below is what the fully mapped Root class would look like. JAXB does not require any annotations (see:, but since the XML elements in your document do not match the default naming rules some annotations are required.

I do not believe this use case can be handled by XStream. If you are open to using other technologies, below is an example of how it could be done with MOXy. Using the @XmlPath extension.

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

To specify MOXy as your JAXB provider you need to add a file called in the same package as your domain classes with the following entry (see:

You can use the following demo code to prove that everything works: