javaxmljaxbunmarshallingpackage-info

JAXB unmarshall xml without package-info.java


JAXB unmarshaller throws exception if there is no package-info.java file. This code is called from another language and custom classloader can not load package-info properly.

I had already manually added the namespace parameter to all the @XmlElement annotations.

Xml root element has multiple xmlns attributes. Two of them (xmlns and xmlns:c) have the same value (I can not change xml, which is comeing from external service)

BUT: It works if I remove xmlns="urn:foo:bar" from document even without package-info

The question: how can I unmarshall without package-info (I can not modify custom classloader which can not load it) and without removing xmlns from xml ? What should I change in my code?

Java 1.6_45 vJAXB 2.1.10

XML root element sample:

<?xml-stylesheet type="text/xsl" href="file1.xslt">
<?xml-stylesheet type="text/xsl" href="file1.xslt"?>
<c:ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="urn:foo:ba" xmlns:c="urn:foo:bar" xmlns:std="urn:foo:std"
 xsi:schemaLocation="urn:foo:bar file2.xsd">

Code:

String path = "D:\\data\\document.xml";

    try {
        JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller u = jc.createUnmarshaller();
        Object root = u.unmarshal(new File(path)); //exception w/o package-info.java

        CDocument doc= (CDocument) JAXBIntrospector.getValue(root);
        System.out.println(doc);

package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "urn:foo:bar", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    xmlns={@XmlNs(namespaceURI = "urn:foo:bar", prefix = "")})
package test.model;

    import javax.xml.bind.annotation.XmlNs;

Exception:

javax.xml.bind.UnmarshalException: Unable to create an instance of my.package.here.model.ANY

Any ideas will be greatly appreciated, I had spent so much time trying, but still have nothing.


Solution

  • BUT: It works if I remove xmlns="urn:foo:bar" from document even without package-info
    

    This means that you have not added the namespace parameter everywhere you need to.

    In the XML document below without the @XmlSchema annotation to map the namespace qualification, you would require the namespace parameter on the @XmlRootElement annotation for the Foo class and the @XmlElement annotation on the bar property.

    <foo xmlns="http://www.example.com">
        <bar>Hello World</bar>
    </foo>