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: Neha <neha.srivastava8_at_gmail.com>
Date: Wed, 9 Jan 2013 05:54:07 -0800 (PST)

Thanks a lot!!

Your input was very useful. It started working when I removed the prefix,
and the trailing '/' in the URL.

On Wed, Jan 9, 2013 at 1:05 PM, Wolfgang Laun-2 [via GlassFish] <
ml-node+s10926n87478h41_at_n7.nabble.com> wrote:

> (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 <[hidden email]<http://user/SendEmail.jtp?type=node&node=87478&i=0>>
> 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.
> >
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://glassfish.10926.n7.nabble.com/javax-xml-bind-UnmarshalException-unexpected-element-uri-http-xmlns-escenic-com-2008-content-type-lo-tp87475p87478.html
> To unsubscribe from javax.xml.bind.UnmarshalException: unexpected element
> (uri:"http://xmlns.escenic.com/2008/content-type",
> local:"content-types"). Expected elements are <{}content-types>, click
> here<http://glassfish.10926.n7.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=87475&code=bmVoYS5zcml2YXN0YXZhOEBnbWFpbC5jb218ODc0NzV8MTMyMjM0MTE5MQ==>
> .
> NAML<http://glassfish.10926.n7.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
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-tp87475p87480.html
Sent from the JAXB users mailing list archive at Nabble.com.