Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.xml.bind.UnmarshalException

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

like image 542
dreambigcoder Avatar asked Apr 01 '13 20:04

dreambigcoder


People also ask

What is UnmarshalException?

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.

How do I set namespace prefix in JAXB?

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.

What is @XmlRootElement in Java?

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 .


1 Answers

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

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
like image 130
bdoughan Avatar answered Oct 23 '22 20:10

bdoughan