unmarshal XML from String using CastorXML

Discussion in 'Java' started by crash.test.dummy, Jan 29, 2006.

  1. 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.
    crash.test.dummy, Jan 29, 2006
    #1
    1. Advertising

  2. crash.test.dummy

    Tim B Guest

    "crash.test.dummy" <> wrote in message
    news:...
    > 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.
    >


    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();
    }
    }
    }
    Tim B, Jan 30, 2006
    #2
    1. Advertising

  3. i'll try this one out. thanks for the big help!
    crash.test.dummy, Jan 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Serge Corbucci
    Replies:
    0
    Views:
    478
    Serge Corbucci
    Jan 14, 2004
  2. leo

    marshal and unmarshal

    leo, Aug 26, 2006, in forum: Python
    Replies:
    1
    Views:
    329
    Fredrik Lundh
    Aug 27, 2006
  3. Dmitri Fedoruk
    Replies:
    4
    Views:
    690
    Dmitri Fedoruk
    Sep 14, 2007
  4. Replies:
    4
    Views:
    182
    Eric Hodel
    Aug 20, 2007
  5. CatchSandeepVaid

    Unmarshal element as String

    CatchSandeepVaid, Mar 1, 2012, in forum: Java
    Replies:
    0
    Views:
    272
    CatchSandeepVaid
    Mar 1, 2012
Loading...

Share This Page