UnmarshalException - namespace problem?

I get the following exception when I try to revert XML back to java code. It seems like I'm missing the namespace declaration somewhere, but I'm 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 created using the 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 top XML file that should be unreinforced:

 <?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 Java file created by JAXB:

 @XmlRootElement(name = "wg_provider" ) public class WgProvider { 

I tried adding a namespace element to Annotation, but that didn't matter.

Here is the contents of package-info.xml:

 @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, this 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

PS I am using Netbeans 7 to create bindings.

+6
source share
1 answer

In your example, you need to specify DocumentBuilderFactory as a namespace:

 DocumentBuilderFactory documentbuilderFactory = DocumentBuilderFactory.newInstance(); documentbuilderFactory.setNamespaceAware(true); 

You can also disable ByteArrayInputStream directly:

 unmarshaller.unmarshal(xmlStream); 
+4
source

Source: https://habr.com/ru/post/906327/


All Articles