unmarshal XML from String using CastorXML

C

crash.test.dummy

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.
 
T

Tim B

crash.test.dummy said:
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();
}
}
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top