users@jaxb.java.net

Re: Trouble/confusion unmarshalling: Invalid content was found starting with element 'foo'. One of '{foo}' is expected.

From: Malachi de Ælfweald <malachid_at_gmail.com>
Date: Wed, 8 Sep 2010 08:51:00 -0700

Looking at your XSD, it's a bit different than I would have expected.

For example, instead of:
<schema version="1.0" targetNamespace="http://ns.myorg.org/gsearch"
       xmlns:this="http://ns.myorg.org/gsearch" xmlns="
http://www.w3.org/2001/XMLSchema">

I would have done:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://ns.myorg.org/gsearch"
  xmlns="http://ns.myorg.org/gsearch"
  jaxb:version="2.0"
  elementFormDefault="qualified">


And instead of:
       <element name="resultPage">
               <complexType>
                       <all>
                               <element name="gfindObjects"
type="this:gfindObjects"/>
                       </all>
                       <attribute name="indexName" type="string"/>
                       <attribute name="dateTime" type="string"/>
               </complexType>
       </element>

I would have done:
<xsd:element name="resultPage" type="Result" />

<xsd:complexType name="Result">
  <xsd:sequence>
    <xsd:element name="gFindObjects" type="GFind" minOccurs="0"
maxOccurs="unbounded"/>
  </xsd:sequence>
  <xsd:attribute name="indexName" type="xsd:string"/>
  <xsd:attribute name="dateTime" type="xsd:string"/>
</xsd:complexType>

(of course, with some annotations thrown in for better name mapping)

Malachi de Ælfweald
http://www.google.com/profiles/malachid


On Wed, Sep 8, 2010 at 7:48 AM, Josh Endries <josh_at_endries.org> wrote:

> Hello,
>
> I'm having trouble unmarshalling an XML document that comes from another
> app:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <resultPage xmlns="http://ns.myorg.org/gsearch">
> <gfindObjects>
> <objects>
> <object no="1" score="1.4655163">
> <field name="PID">hdl:2200%2F20061003061239460T</field>
> </object>
> </objects>
> </gfindObjects>
> </resultPage>
>
> The actual document is huge, this is a stripped-down version. I'm no XML
> expert but this seems like a normal XML document, even with a specified
> default namespace. Here is the schema I made to match it:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <schema version="1.0" targetNamespace="http://ns.myorg.org/gsearch"
> xmlns:this="http://ns.myorg.org/gsearch" xmlns="
> http://www.w3.org/2001/XMLSchema">
>
> <element name="resultPage">
> <complexType>
> <all>
> <element name="gfindObjects"
> type="this:gfindObjects"/>
> </all>
> <attribute name="indexName" type="string"/>
> <attribute name="dateTime" type="string"/>
> </complexType>
> </element>
>
> <complexType name="gfindObjects">
> <sequence>
> <element name="objects" type="this:objects"/>
> </sequence>
> <attribute name="hitTotal" type="int"/>
> <attribute name="resultPageXslt" type="string"/>
> <attribute name="hitPageSize" type="int"/>
> <attribute name="hitPageStart" type="int"/>
> <attribute name="query" type="string"/>
> </complexType>
>
> <complexType name="objects">
> <sequence>
> <element name="object" type="this:object"
> maxOccurs="unbounded" minOccurs="0"/>
> </sequence>
> </complexType>
>
> <complexType name="object">
> <sequence>
> <element name="field" type="this:field"
> maxOccurs="unbounded" minOccurs="0"/>
> </sequence>
> <attribute name="no" type="int"/>
> <attribute name="score" type="decimal"/>
> </complexType>
>
> <complexType name="field">
> <simpleContent>
> <extension base="string">
> <attribute name="name"/>
> </extension>
> </simpleContent>
> </complexType>
> </schema>
>
> As far as I understand namespaces, this should work...but I get:
>
> DefaultValidationEventHandler: [FATAL_ERROR]: cvc-complex-type.2.4.a:
> Invalid content was found starting with element 'gfindObjects'. One of
> '{gfindOb
> jects}' is expected.
>
> I don't know what this means. I guess it isn't considering gfindObjects as
> declared to be in the default namespace but in the XML doc it isn't in any
> namespace. It does apparently find resultPage though. I tried adding a
> prefix to resultPage and the xmlns attribute but that didn't help. I've
> tried all sorts of other things too, too many to list (or remember).
>
> This is my code; I saved the xsd and xml locally so I don't have to keep
> doing network calls:
>
> /*
> * Do the actual unmarshalling.
> */
> gSearchResponse = new FileInputStream("c:/rest.xml");
> JAXBContext gjc =
> JAXBContext.newInstance("org.myorg.ns.transforms.gsearch");
> Unmarshaller gum = gjc.createUnmarshaller();
> SchemaFactory sf =
> SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
> Schema s = sf.newSchema(new File("c:/gsearch.xsd"));
> gum.setSchema(s);
> gum.setEventHandler(new
> javax.xml.bind.helpers.DefaultValidationEventHandler());
> org.myorg.ns.transforms.gsearch.ResultPage rp =
> (org.myorg.ns.transforms.gsearch.ResultPage) gum.unmarshal(gSearchResponse);
>
> Can anyone help? Is this enough information?
>
> Thanks,
> Josh
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>