I am getting the following error:
javax.xml.bind.UnmarshalException: unexpected element(uri:"http://www.docsite.com/ClientConfig.xsd", local:"ClientConfig").
Expected elements are <{http://www.docsite.com/ClientConfig.xsd/}ClientConfig>
my root element class file is:
@XmlRootElement(name="ClientConfig",namespace="http://www.docsite.com/ClientConfig.xsd/")
public class ClientConfig {}
my package.info file is:
@XmlSchema(namespace="http://www.docsite.com/ClientConfig.xsd",elementFormDefault=XmlNsForm.QUALIFIED)
package com.convertXml.docSite.XmlConverter;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;
let me know what can I do to fix this
public class UnmarshalException extends JAXBException. This exception indicates that an error has occurred while performing an unmarshal operation that prevents the JAXB Provider from completing the operation. The ValidationEventHandler can cause this exception to be thrown during the unmarshal operations.
If you are using the default JAXB implementation provided with Java 6 or later, you can configure the namespace prefixes by extending the NamespacePrefixMapper and setting a property to tell the marshaller to use your extension.
When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .
TL;DR
You have an extra / at the end of the namespace specified in the @XmlRootElement
annotation.
LONG ANSWER
package-info
The namespace is specified correctly in the package level @XmlSchema
annotation:
@XmlSchema(namespace="http://www.docsite.com/ClientConfig.xsd",elementFormDefault=XmlNsForm.QUALIFIED)
package com.convertXml.docSite.XmlConverter;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;
ClientConfig
But you have overridden it with an incorrect namespace on the ClientConfig
class. You have an extra /
at the end of the namespace specified in the @XmlRooElement
annotation.
package com.convertXml.docSite.XmlConverter;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="ClientConfig",namespace="http://www.docsite.com/ClientConfig.xsd/")
public class ClientConfig {}
Since you declared the namespace on the @XmlSchema
on the package-info
class you don't need to repeat it on the @XmlRootElement
.
package com.convertXml.docSite.XmlConverter;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="ClientConfig")
public class ClientConfig {}
Demo
Now the unmarshal
will work correctly:
package com.convertXml.docSite.XmlConverter;
import java.io.StringReader;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ClientConfig.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader xml = new StringReader("<ClientConfig xmlns='http://www.docsite.com/ClientConfig.xsd'/>");
ClientConfig clientConfig = (ClientConfig) unmarshaller.unmarshal(xml);
}
}
For More Information
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