Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-problematic XML-to-Java binding

I've looked all over the place for a good way to bind XML elements to Java classes. It sounds like a fairly easy thing to implement (for some value of easy), but wherever I look, there's the need for XSDs, explicit mapping and a bunch of other things that really get in the way for me.

This is my (possibly incomplete) idea of a Java object representing a sitemap.

public class Sitemap {

    private List<Url> urls;

    public List<Url> getUrls() {
        return urls;
    }

    public void setUrls(List<Url> urls) {
        this.urls = urls;
    }
}

I'm looking for something which lets me do:

Sitemap sitemap = (Sitemap) Foo.fromXml("sitemap.xml")

Something like that, or at least similar. Is there any such thing?

like image 308
damd Avatar asked Sep 24 '26 03:09

damd


1 Answers

Below is how this could be done with a JAXB (JSR-222). An implementation is included in Java SE 6. There are also other implementations such as EclipseLink MOXy (I'm the tech lead).

SHORT ANSWER

You couls use the following API call with JAXB with any annotations or XML schemas.

Sitemap sitemap = JAXB.unmarshal(xml, Sitemap.class);

LONG ANSWER

Below is a more detailed example.

Sitemap

I've modified your class slightly. I wasn't sure what the Url class was so I changed it to java.net.URL. Note how no annotations are required on the domain model.

package forum10854001;

import java.net.URL;
import java.util.List;

public class Sitemap {

    private List<URL> urls;

    public List<URL> getUrls() {
        return urls;
    }

    public void setUrls(List<URL> urls) {
        this.urls = urls;
    }
}

Demo

Instead of the code used in the short answer, I have created a JAXBContext. The JAXBContext is a thread safe object that represents all the initialized metadata. The Marshaller and Unmarshaller objects provider additional flexibility over the JAXB class.

The code below demonstrates how to read in the XML and them write it back out:

package forum10854001;

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource xml = new StreamSource("src/forum10854001/input.xml");
        JAXBElement<Sitemap> jaxbElement = unmarshaller.unmarshal(xml, Sitemap.class);

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

    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<sitemap>
    <urls>http://www.eclipse.org/eclipselink/moxy.php</urls>
    <urls>http://jaxb.java.net</urls>
</sitemap>

For More Information

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
like image 184
bdoughan Avatar answered Sep 26 '26 17:09

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!