简体   繁体   中英

JAXB Unmarshalling using XMLSchema not working with several namespaces

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.

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!

Both @XmlRootElement and @XmlElement has a namespace attribute. When not defined, the namespace of the package, as defined in package-info.java , is used.

Since you didn't specify the attribute, all your elements belong to namespace http://test.org/url1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM