f



Again with JAXB (better.. AGAINST it :D :D)

Ok, I start here a new post, because I ran into some more complex
problems :(.

I got an XML schema, binded via xjc.sh:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0">


  <xs:element name="Resource" type="MyResource"/>


  <xs:complexType name="MyResource">
      <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element name="FirstTag" type="xs:string"/>
        <xs:element name="SecondTag" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>

</xs:schema>


and a XML Document:
<?xml version="1.0" encoding="utf-8"?>

<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="newschema.xsd">

  <FirstTag>MyFirstValue</FirstTag>
  <SecondTag>MySecondValue</SecondTag>

</Resource>

Following the API :
1   ObjectFactory obj = new ObjectFactory();
2   JAXBContext jc = JAXBContext.newInstance( "newschema" );
3   Unmarshaller u = jc.createUnmarshaller();
4   MyResource r = (MyResource)u.unmarshal(new File( "template.xml" ));
5   Marshaller m = jc.createMarshaller();
6   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
7   OutputStream os = new FileOutputStream( "newtempl.xml" );
8   m.marshal( r, os );

The result is a ClassCastException.
Replacing line 4 and 8 with:

4b Object o = u.unmarshal( new File( "template.xml" ) );
8b m.marshal (o, os);

all works fine.

So, my question are:
1) How to cast the object o?
2) Since the setValidation(true) is deprecated, how to validate?

Thanks a lot!

0
jne (10)
6/14/2006 12:17:53 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

2 Replies
356 Views

Similar Articles

[PageSpeed] 38

marco wrote:

> Following the API :
> 1   ObjectFactory obj = new ObjectFactory();
> 2   JAXBContext jc = JAXBContext.newInstance( "newschema" );
> 3   Unmarshaller u = jc.createUnmarshaller();
> 4   MyResource r = (MyResource)u.unmarshal(new File( "template.xml" ));
> 5   Marshaller m = jc.createMarshaller();
> 6   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
> 7   OutputStream os = new FileOutputStream( "newtempl.xml" );
> 8   m.marshal( r, os );
>
> The result is a ClassCastException.
> Replacing line 4 and 8 with:

With given schema and xml, the above code should work fine. I have
generated stubs with xjc shipped with jwsdp-1.6. I didn't get
ClassCastException with above code.

Rao

0
nkalagarla (14)
6/14/2006 4:52:56 PM
Thanks a lot, it's solved!
I was using JAXB 2.0, and not jwsdp-1.6.

Thanks again.

marco

nkalagarla@gmail.com ha scritto:

> marco wrote:
>
> > Following the API :
> > 1   ObjectFactory obj = new ObjectFactory();
> > 2   JAXBContext jc = JAXBContext.newInstance( "newschema" );
> > 3   Unmarshaller u = jc.createUnmarshaller();
> > 4   MyResource r = (MyResource)u.unmarshal(new File( "template.xml" ));
> > 5   Marshaller m = jc.createMarshaller();
> > 6   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
> > 7   OutputStream os = new FileOutputStream( "newtempl.xml" );
> > 8   m.marshal( r, os );
> >
> > The result is a ClassCastException.
> > Replacing line 4 and 8 with:
>
> With given schema and xml, the above code should work fine. I have
> generated stubs with xjc shipped with jwsdp-1.6. I didn't get
> ClassCastException with above code.
> 
> Rao

0
jne (10)
6/15/2006 9:41:04 AM
Reply: