users@jaxb.java.net

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

From: Josh Endries <josh_at_endries.org>
Date: Wed, 08 Sep 2010 10:48:08 -0400

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