3

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?

bdoughan
  • 147,609
  • 23
  • 300
  • 400
glebreutov
  • 785
  • 1
  • 7
  • 18

4 Answers4

5

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>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
4

Try JAXB - http://jaxb.java.net/

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

aar
  • 322
  • 1
  • 7
1

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
  • 876
  • 3
  • 7
  • 17
  • 1
    Is Betwixt an active project? The date on the documentation on the last release 0.8 is dated December 2006. – bdoughan Sep 16 '10 at 19:25
  • 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 Sep 16 '10 at 20:50
  • For advanced binding check out MOXy - http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html – bdoughan Sep 17 '10 at 18:06
1

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
  • 266,786
  • 75
  • 396
  • 414
  • 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. – bdoughan Sep 16 '10 at 20:24