Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
marco - 14 Jun 2006 13:17 GMT
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!
nkalagarla@gmail.com - 14 Jun 2006 17:52 GMT
> Following the API :
> 1   ObjectFactory obj = new ObjectFactory();
[quoted text clipped - 8 lines]
> 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
marco - 15 Jun 2006 10:41 GMT
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:

> > Following the API :
> > 1   ObjectFactory obj = new ObjectFactory();
[quoted text clipped - 14 lines]
>
> Rao


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.