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

M

marco

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!
 
N

nkalagarla

marco said:
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
 
M

marco

Thanks a lot, it's solved!
I was using JAXB 2.0, and not jwsdp-1.6.

Thanks again.

marco

(e-mail address removed) ha scritto:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top