Binding XML using JAXB and Spring -MVC

The following XML file is in the repository on the 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 the tutorial, I saw that I should use @XmlSchema for package-info.java. I create the package-info.java class, but I don’t know how to treat it.

Actually my problem is that I do not know how to use corect annotations to bind xml to the model class. The whole story is that I'm trying to extract an XML document from the repository, but I accept null values. The problem I saw here: JAXB: how to associate an element with a namespace that I do not use the correct annotations. Does anyone know which correct annotations are and how to use them?

+3
source share
2

, :

-

@XmlSchema, . namespace ("http://www.../ckp"). , XML, elementFormDefault=XmlNsForm.QUALIFIED. xmlns URI .

@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. (, ).

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);
    }

}

<?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>

+3

namespace @XmlRootElement, ; , @XmlAttribute, author, author , .

:

, , corect xml .

spring :

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

jaxb2Marshaller MessageConverter :

<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>

spring, AnnotationMethodHandlerAdapter :

<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>
0

All Articles