I have been looking across the site searching for a examples on how to implement Unmarshalling using the annotation @XMLSchema on the package-info.java file.
My understanding is that the theory suppose to be really simple. However, after look at the following links I still don't figure our why the annotation is not working.
- Links that I used for reference:
- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
- JAXB XmlSchema.xmlns (in package-info.java) ignored by marshaller
- @xmlschema jaxb package-info.java compilation error -http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
Here is the code:
package-info.java:
@XmlSchema(
namespace="http://test.org/url1",
elementFormDefault = XmlNsForm.QUALIFIED,
attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
xmlns={
@XmlNs(prefix="ns6", namespaceURI="http://test.org/url6"),
@XmlNs(prefix="ns5", namespaceURI="http://test.org/url5"),
@XmlNs(prefix="ns7", namespaceURI="http://test.org/url7"),
@XmlNs(prefix="ns1", namespaceURI="http://test.org/url1"),
@XmlNs(prefix="ns3", namespaceURI="http://test.org/url3"),
@XmlNs(prefix="ns2", namespaceURI="http://test.org/url2"),
@XmlNs( prefix="ns4", namespaceURI="http://test.org/url4"),
}
)
package simpleJAXB;
import javax.xml.bind.annotation.*;
The XML used as input:
<?xml version="1.0" encoding="UTF-8"?>
<ns6:RequestShort
xmlns:ns2="http://test.org/url2"
xmlns:ns1="http://test.org/url1"
xmlns:ns4="http://test.org/url4"
xmlns:ns3="http://test.org/url3"
xmlns:ns5="http://test.org/url5"
xmlns:ns6="http://test.org/url6"
xmlns:ns7="http://test.org/url7">
<ns3:IndividualRequestShort>
<ns2:Person>Name1</ns2:Person>
<ns2:Title>TEST</ns2:Title>
</ns3:IndividualRequestShort>
<ns3:IndividualRequestShort>
<ns2:Person>Name2</ns2:Person>
<ns2:Title>TEST2</ns2:Title>
</ns3:IndividualRequestShort>
</ns6:RequestShort>
Here are the classes as well:
RequestShort:
package simpleJAXB;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement ( name = "RequestShort")
public class RequestShort {
ArrayList<IndividualRequestShort> individualRequestListShort;
public RequestShort(){}
@XmlElement(name = "IndividualRequestShort", type=IndividualRequestShort.class)
public ArrayList<IndividualRequestShort> getIndividualRequestListShort() {
return individualRequestListShort;
}
public void setIndividualRequestListShort(
ArrayList<IndividualRequestShort> individualRequestList) {
this.individualRequestListShort = individualRequestList;
}
}
IndividualRequestShort:
package simpleJAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name= "IndividualRequestShort")
public class IndividualRequestShort {
String title;
String person;
public IndividualRequestShort(){}
@XmlElement(name = "Title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@XmlElement(name = "Person")
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
Finally, the main void method is :
try{
File file = new File("XML_test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(RequestShort.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
RequestShort requestShort = (RequestShort) jaxbUnmarshaller.unmarshal(file);
ArrayList<IndividualRequestShort> contentRequests = new ArrayList<IndividualRequestShort>();
contentRequests = requestShort.getIndividualRequestListShort();
if(contentRequests != null){
Iterator<IndividualRequestShort> requestsIterator = contentRequests.iterator();
IndividualRequestShort temp = null;
while (requestsIterator.hasNext()) {
temp = (IndividualRequestShort)requestsIterator.next();
System.out.println("Data: " + temp.getTitle() + temp.getPerson());
}
} catch (Exception e) {
System.out.println(e.toString() + " " + e.getStackTrace().toString());
}
return null;
}
I have been able to actually parse the xml on the xmlelment and xmlrootelement with the namespace option. However, when I am trying to put the xmlschema annotation, as I show above, I receive the following error:
Classes known to this context:
[B
boolean
byte
char
com.sun.xml.internal.bind.api.CompositeStructure
double
float
int
java.awt.Image
java.io.File
java.lang.Boolean
java.lang.Byte
java.lang.Character
java.lang.Class
java.lang.Double
java.lang.Float
java.lang.Integer
java.lang.Long
java.lang.Object
java.lang.Short
java.lang.String
java.lang.Void
java.math.BigDecimal
java.math.BigInteger
java.net.URI
java.net.URL
java.util.Calendar
java.util.Date
java.util.GregorianCalendar
java.util.UUID
javax.activation.DataHandler
javax.xml.bind.JAXBElement
javax.xml.datatype.Duration
javax.xml.datatype.XMLGregorianCalendar
javax.xml.namespace.QName
javax.xml.transform.Source
long
short
simpleJAXB.IndividualRequestShort
simpleJAXB.RequestShort
void
Do I am missing something here? Can you give me a hint ?
Thank you in advance!
PS: Sorry I am asking something so simple, but I am learning this JAXB framework.
Regards!