XML binding with the use of JAXB and Spring-MVC
annotations, java, jaxb, spring-mvc, xsd
Solution
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
- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
- http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html
Problem
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?