I get the following exception when I attempt to unmarshall XML back into java code. It seems like I am missing the namespace declaration somewhere, but I am not sure where.
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"wg_provider").
Expected elements are <{http://www.warriorgateway.org/WGProvider.xsd}wg_provider>
This file was originally created with a JAXB marshaller here:
    JAXBContext providerContext = JAXBContext.newInstance(WgProvider.class);
    Marshaller providerMarshaller = providerContext.createMarshaller();
    providerMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter providerWriter = new StringWriter();
    providerMarshaller.marshal(provider, providerWriter);
Here is the top of the Schema file:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.warriorgateway.org/WGProvider.xsd"
    xmlns:ns1="http://www.warriorgateway.org/WGProvider.xsd"
    elementFormDefault="qualified">
    <xsd:element name="wg_provider">
        <xsd:complexType>
Here is the the top of the XML file to be unmarshalled:
<?xml version="1.0" encoding="UTF-8"?>
<wg_provider xmlns="http://www.warriorgateway.org/WGProvider.xsd">
    <ein>26-0081701</ein>
    <name>MOMS CLUB</name>
    <is_virtual>false</is_virtual>
    <description>
</description>
Here is the top of the JAXB generated Java file:
@XmlRootElement(name = "wg_provider" )
public class WgProvider {
I tried adding the namespace element to the Annotation but it made no difference.
Here is the package-info.xml content:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.warriorgateway.org/WGProvider.xsd", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.warriorgateway.api.model.wg;
And finally here is the unmarshalling code:
for (String wrappedProviderXML : wrappedProviderXMLList) {
    DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentbuilder = documentbuilderFactory.newDocumentBuilder();
    ByteArrayInputStream xmlStream = new ByteArrayInputStream(wrappedProviderXML.getBytes());
    Document providerXMLDocument = documentbuilder.parse(xmlStream);
    JAXBContext wgProviderContext;
    try {
        wgProviderContext = JAXBContext.newInstance(WgProvider.class);
        Unmarshaller wgProviderUnmarshaller = wgProviderContext.createUnmarshaller();
        WgProvider wgProvider = (WgProvider) wgProviderUnmarshaller.unmarshal(providerXMLDocument);
    } catch (JAXBException ex) {
        Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
    }
Thanks-in-advance,
Guido
P.S. I am using Netbeans 7 to generate the bindings.
In your sample you need to set your DocumentBuilderFactory to be namespace aware:
DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance();
documentbuilderFactory.setNamespaceAware(true);
You could also unmarshal the ByteArrayInputStream directly:
unmarshaller.unmarshal(xmlStream);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With