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 / January 2006

Tip: Looking for answers? Try searching our database.

unmarshal XML from String using CastorXML

Thread view: 
crash.test.dummy - 29 Jan 2006 22:41 GMT
Hi all,
  I need to unmarshal an XML but the XML data is not in a file but in
a String:
String xml = "<person><name>John
Doe</name><birthdate>12/12/1986</birthdate></person>".

the actual XML came from an MQ, hence it is not in a file. (can't make
it to a file since mainframe is the one sending this to MQ).

Is there a way to do this using Castor, and not creating a temporary
XML file? If none, any suggestions?

Thanks.
Tim B - 30 Jan 2006 02:17 GMT
> Hi all,
>    I need to unmarshal an XML but the XML data is not in a file but in
[quoted text clipped - 9 lines]
>
> Thanks.

Yes, there is a way to do it. Here's an example. I don't know if this is the
best way to do it, but it works.

package mypackage1;
import java.io.StringReader;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.apache.xml.serialize.XMLSerializer;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

public class Tester  {

   public static void main(String[] args) {
      String xml = "<DataBean><value1>foo</value1></DataBean>";
      StringReader sr = new StringReader(xml);
      InputSource is = new InputSource(sr);
      Unmarshaller um = new Unmarshaller();
      try {
          DataBean dataBean = (DataBean)um.unmarshal(DataBean.class, is);
          System.out.println("value1 is " + dataBean.getValue1());
      }
      catch (MarshalException e) {
          e.printStackTrace();
      }
      catch (ValidationException e) {
           e.printStackTrace();
      }
   }
}
crash.test.dummy - 30 Jan 2006 02:26 GMT
i'll try this one out. thanks for the big help!


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



©2009 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.