users@jaxb.java.net

can't unmarshal a marshalled Element

From: Mark Hansen <khookguy_at_yahoo.com>
Date: Tue, 29 Jun 2004 13:40:42 -0700 (PDT)

I get a "Default ValidationEventHandler [ERROR]:
Unexpected element" when I try to unmarshal XML that
has been created by the JAXB marshaller.

The cause seems to be setting a value using an JAXB
"Element" instance instead of a "Type" instance. But
I don't understand why this should be a problem since
both of these generated classes implement that "Type"
interface. See details below.

Here is the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="http://www.example.com/test"
  xmlns:test="http://www.example.com/test"
  xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="outer" type="test:OuterType"/>
  <xs:element name="inner" type="test:InnerType"/>
  <xs:complexType name="OuterType">
    <xs:sequence>
      <xs:element name="value" type="xs:string"
minOccurs="0" maxOccurs="1"/>
      <xs:element name="inner" type="test:InnerType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="InnerType">
    <xs:sequence>
      <xs:element name="value" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>


Here is the code:

    ObjectFactory objFac = new ObjectFactory();
    Outer out = objFac.createOuter();
    Inner in = objFac.createInner();
    // if "InnerType" (below) is used then no ERROR!
    // InnerType in = objFac.createInnerType();
    in.setValue("some value");
    out.setValue("an outer value");
    out.setInner(in);
    Marshaller m = objFac.createMarshaller();
    m.marshal(out, System.out);
    Unmarshaller u = objFac.createUnmarshaller();
    DocumentBuilderFactory dbf =
      DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    m.marshal( out, doc );
    Object obj = u.unmarshal(doc);
    System.out.println
      ("unmarshaled instanceof = " + obj.getClass());


                
__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net