views:

63

answers:

4

In my application i use some API via HTTP and it returns responces as xml. I want automaticaly bind data from xml to beans.

For example bind following xml:

<xml>
   <userid>123456</userid>
   <uuid>123456</uuid>
</xml>

to this bean (maybe with help of annotations)

class APIResponce implement Serializable{

private Integer userid;
private Integer uuid;
....
}

What the simplest way to do this?

+4  A: 

Try JAXB - https://jaxb.dev.java.net/

An intro article for it http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html

aar
You can even generate your beans from a schema, etc.
Peter DeWeese
+1  A: 

As an alternative to Castor and JAXB, Apache also has a project for doing XML to object binding:

Betwixt: http://commons.apache.org/betwixt/

skel625
Is Betwixt an active project? The date on the documentation on the last release 0.8 is dated December 2006.
Blaise Doughan
I don't believe it is active anymore. I certainly wouldn't recommend it for a large project, but for simple xml to java object binding it works great. We use Castor for our advanced binding needs but found it was a bit of overkill for simple binding needs.
skel625
For advanced binding check out MOXy - http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
Blaise Doughan
+2  A: 

I agree with using JAXB. As JAXB is a spec you can choose from multiple implementations:

Here is how you can do it with JAXB:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class APIResponce {

    private Integer userid; 
    private Integer uuid; 

}

When used with the follow Demo class:

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

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

        File xml = new File("input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        APIResponce api = (APIResponce) unmarshaller.unmarshal(xml);

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

Will produce the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <userid>123456</userid>
    <uuid>123456</uuid>
</xml>
Blaise Doughan
+1  A: 

In the past I have used XMLBeans for binding XML to Java types. It's really easy to use. You first have to compile your xml schema into Java types using the scomp command (or maven plugin etc) and use the types in your code.

There is an example of code in action here.

dogbane
From my understanding XMLBeans has its own proprietary classes and not deal with POJOs. For glebreutov to use his own bean he would need to copy from XMLBeans objects to his class.
Blaise Doughan