Alex Dowining
Alex Dowining

Reputation: 970

XML binding with the use of JAXB and Spring-MVC

the following xml is in a repository on a server:

   <author xmlns="http://www..." xmlns:atom="http://www.w3.org/2005/atom">
     <name>S. Crocker</name>
     <address>None</address>
     <affiliation></affiliation>
     <email>None</email>
   </author>

My model class:

  @XmlRootElement(name = "author", namespace="http://www...")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Author {


    @XmlAttribute(name="author")
    private String author;
    @XmlElement(name="name")
private String name;
    @XmlElement(name="address")
private String address;
    @XmlElement(name="affiliation")
private String affiliation;
    @XmlElement(name="email")
    private String email;



    public String getAuthor() {
    return author;
}
public void setAuthor(String author) {
    this.author = author;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getAffiliation() {
    return affiliation;
}
public void setAffiliation(String affiliation) {
    this.affiliation = affiliation;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

 }

According to a tutorial i saw i should use the @XmlSchema to a package-info.java I create a class package-info.java but i don't know how to treat this.

Actually my problem is that i don't know how to use the corect annotations to bind the xml with the model class. The whole story is that i'm trying to retrieve an XML document from a repository, but i take null values. The problem as i saw here: JAXB: How to bind element with namespace is that i don't use the correct annotations. Does anyone knows which are the correct annotations and how should i use them?

Upvotes: 3

Views: 5164

Answers (2)

bdoughan
bdoughan

Reputation: 149047

Below is an example of how you could map this use case:

package-info

I would use the package level @XmlSchema annotation to specify the namespace qualification. Specify the namespace to be your target namespace ("http://www.../ckp"). You want this namespace applied to all XML elements so specify elementFormDefault=XmlNsForm.QUALIFIED. The use xmlns to asssociate prefixes with your namespace URIs.

@XmlSchema(
    namespace="http://www.../ckp",
    elementFormDefault=XmlNsForm.QUALIFIED,
    xmlns={
        @XmlNs(prefix="", namespaceURI="http://www.../ckp"),
        @XmlNs(prefix="atom", namespaceURI="http://www.w3.org/2005/atom"),
    }
)
package forum10388261;

import javax.xml.bind.annotation.*;

Author

Below is what your Author class would look like. I have removed the unnecessary annotations (annotations that were equivalent to the default mapping).

package forum10388261;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Author {

    @XmlAttribute
    private String author;
    private String name;
    private String address;
    private String affiliation;
    private String email;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAffiliation() {
        return affiliation;
    }

    public void setAffiliation(String affiliation) {
        this.affiliation = affiliation;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

Demo

package forum10388261;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum10388261/input.xml");
        Author author = (Author) unmarshaller.unmarshal(xml);

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

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<author xmlns:atom="http://www.w3.org/2005/atom" xmlns="http://www.../ckp">
    <name>S. Crocker</name>
    <address>None</address>
    <affiliation></affiliation>
    <email>None</email>
</author>

For More Information

Upvotes: 3

beerbajay
beerbajay

Reputation: 20300

I've never needed to use the namespace parameter on @XmlRootElement, try leaving that off; also, you have @XmlAttribute specified for author, but there's no author in your example besides the tag name.

As for:

Actually my problem is that i don't know how to use the corect annotations to bind the xml with the model class.

In your spring config you can do:

<oxm:jaxb2-marshaller id="jaxb2Marshaller">
    <oxm:class-to-be-bound name="com.mycompany.Author"/>
    <!-- ... -->
</oxm:jaxb2-marshaller>

Then inject the jaxb2Marshaller directly or use a MessageConverter like so:

<bean id="xmlConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg>
        <ref bean="jaxb2Marshaller"/>
    </constructor-arg>
    <property name="supportedMediaTypes">
        <list>
            <bean class="org.springframework.http.MediaType">
                <constructor-arg index="0" value="application"/>
                <constructor-arg index="1" value="xml"/>
                <constructor-arg index="2" value="UTF-8"/>
            </bean>
        </list>
    </property>
</bean>

If you want to use spring's content negotiation, you could then use AnnotationMethodHandlerAdapter with the message converter:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="xmlConverter"/>
            <!-- other converters if you have them, e.g. for JSON -->
        </list>
    </property>
</bean>

Upvotes: 0

Related Questions