users@jaxb.java.net

Re: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://xmlns.escenic.com/2008/content-type", local:"content-types"). Expected elements are <{}content-types>

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 9 Jan 2013 14:04:43 +0100

(1) You have to specify the namespace for the root element:
@XmlRootElement(name="content-types",
namespace="http://xmlns.escenic.com/2008/content-type")

(2) Never, ever add a namespace prefix to an element name in the
"name" part of an annotation. (The "ui" is absolutely local to the XML
file, and could be systematically replaced by anything.)

(3) Make sure to use exactly identical URIs (no trailing '/' here):

@XmlElement(name = "label",
                    namespace="http://xmlns.escenic.com/2008/interface-hints")
        protected String label;

-W

On 09/01/2013, Neha <neha.srivastava8_at_gmail.com> wrote:
> Hi,
>
> I am trying to manually write JAXB annotated POJOs to convert my xmls into
> java objects using jaxb and vice versa.
>
> Below is the code
>
> 1)First Java Class - ContentTypes
> ********************************************************************************
> @XmlRootElement(name="content-types")
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(namespace="http://xmlns.escenic.com/2008/content-type")
> public class ContentTypes {
>
> @XmlElement(name="content-type",
> namespace="http://xmlns.escenic.com/2008/content-type")
> protected List<ContentType> contentType;
>
> public List<ContentType> getContentType() {
> if (contentType == null) {
> contentType = new ArrayList<ContentType>();
> }
> return this.contentType;
> }
>
> @Override
> public String toString() {
> return "ContentTypes [contentType=" + contentType + "]";
> }
> }
>
>
> 2)Second Class - ContentType
> ********************************************************************
>
> @XmlAccessorType(XmlAccessType.FIELD)//annotation tells JAXB that only the
> fields will be marshalled to XML
> public class ContentType {
>
> @XmlElement(name = "ui:label",
> namespace="http://xmlns.escenic.com/2008/interface-hints/")
> protected String label;
>
> public String getLabel() {
> return label;
> }
>
> public void setLabel(String label) {
> this.label = label;
> }
> }
>
> 3)Third Java Class - package-info.java
> ***********************************************************************
> @javax.xml.bind.annotation.XmlSchema (
> xmlns = {
> @javax.xml.bind.annotation.XmlNs(prefix = "",
>
> namespaceURI="http://xmlns.escenic.com/2008/content-type"),
> @javax.xml.bind.annotation.XmlNs(prefix = "ui",
>
> namespaceURI="http://xmlns.escenic.com/2008/interface-hints"),
>
> },
> elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
>
> )
> package contentTypePojo;
>
>
>
> 4)jaxb.index - This has these two entries
> ***********************************************************************
> ContentTypes
> ContentType
>
>
>
> 5)content-type.xml
> *************************************************************************
> <?xml version="1.0"?>
> <content-types xmlns="http://xmlns.escenic.com/2008/content-type"
> xmlns:ui="http://xmlns.escenic.com/2008/interface-hints">
> <content-type name="article">
> <ui:label>Article</ui:label>
> </content-type>
> </content-types>
>
>
> 6)Test class - I am using java main to run the above code
> ***********************************************************************
> public class Test {
>
> public static void main(String[] args) throws JAXBException {
> // TODO Auto-generated method stub
> JAXBContext jc = JAXBContext.newInstance("contentTypePojo");
> Unmarshaller unmarshaller = jc.createUnmarshaller();
>
> unmarshaller.setEventHandler(new
> javax.xml.bind.helpers.DefaultValidationEventHandler());
> File xml = new File("c:/content-type.xml");
> ContentTypes contentTypes = (ContentTypes)
> unmarshaller.unmarshal(xml);
> System.out.println(contentTypes);
> }
>
> }
>
>
> I am getting the below error:
> ***************************************************************
> DefaultValidationEventHandler: [FATAL_ERROR]: unexpected element
> (uri:"http://xmlns.escenic.com/2008/content-type", local:"content-types").
> Expected elements are <{}content-types>
> Location: line 2 of file:/c:/content-type.xml
> Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected
> element (uri:"http://xmlns.escenic.com/2008/content-type",
> local:"content-types"). Expected elements are <{}content-types>
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
>
>
> I have looked on net for all possible forums and content, but the code does
> not work. Can you please help me to figure out what I am doing wrong
>
>
>
> --
> View this message in context:
> http://glassfish.10926.n7.nabble.com/javax-xml-bind-UnmarshalException-unexpected-element-uri-http-xmlns-escenic-com-2008-content-type-lo-tp87475.html
> Sent from the JAXB users mailing list archive at Nabble.com.
>