0

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!

Community
  • 1
  • 1
Ageous
  • 13
  • 4

1 Answers1

0

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.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • But then, how to make use of the array of namespaces inside? I thought since were in the annotation ( xmlns={...} ) these will be taken. If that's not the case, then what's the purpose of this specific option? – Ageous Aug 24 '15 at 19:31
  • @Ageous To pre-declare the namespaces on the `RequestShort` element, so they don't have to be repeated wherever used. Try running with and without to see difference (once you get it working). – Andreas Aug 24 '15 at 19:45
  • Actually, before I posted this, I used the namespace attribute on the XMLRootElement and XMLElement. That worked. However, I thougth that using the XMLSchema will allow me to put everything on single place, not across all my classes. What I posted is just a simplified version of my xml. The real one will have too many more. That's why I wanted to put 1 file with all the namespaces, easy to maintain. However, if this is not possible, if always I have to use the namespace attribute, I don't see what's the purpose of the Xmlns inside the Xmlschema. – Ageous Aug 24 '15 at 20:38
  • @Ageous I would say that a general good practice would be to put Classes for different namespaces into different Packages. That's how the Schema-to-Java generator works. In that case, you'd still only declare the namespaces in `package-info.java`, you'd just have more of them. – Andreas Aug 24 '15 at 20:51