JAXB. Unmarshalling strings XML’s

Hi,

In this article I will show how to unmarshall XML string using JAXB, Usually JAXB uses InputStreams and OutputStreams for the XML text input and output, respectively.

One of workaround by passing a ByteArrayInputStreams (java.io) or ByteArrayOutputStreams (java.io) for XML String  or Using StreamSource to read the input via StringReader.

You can simply read my previous article “Sample on JAXB” to get the complete instruction on building a sample project on JAXB with eclipse plugin.

Below is the method which we can use to perform the unmarshalling of XML String into Java JAXB objects.
1. Via ByteArrayInputStream

public List loadObjectFromXMLString(String xmlString){
try{
ByteArrayInputStream input = new ByteArrayInputStream (xmlString.getBytes());
Object jaxbObject = unmarshaller.unmarshal( input);
if(items == null) {
items = (ItemsType)(((JAXBElement)jaxbObject).getValue());
return(items.getItem());
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}

2 Via StreamSource

JAXBContext jc = JAXBContext.newInstance( “com.acme.foo” );
Unmarshaller u = jc.createUnmarshaller();
StringBuffer xmlStr = new StringBuffer( “<?xml version=”1.0″?>…” );
Object o = u.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) );

[NOTE] This example is totally based on my previous article “Sample on JAXB”. Simply add the above method/statements if your building the project on my previous article instructions. Even there are many ways to do the same thing mentioned in the article. please go through with the below URL.
http://download.oracle.com/javase/6/docs/api/javax/xml/bind/Unmarshaller.html#supportedProps


Thanks
R Vashi

8 thoughts on “JAXB. Unmarshalling strings XML’s

  1. HI,

    I have a query regarding session Management, as I was not sure where to post, I am using this blog …

    We have Single Sign on for our aplication. After session timedout I am redirecting the app to static page saying Session is timed out.BUt If I provide a login link in the same page, it does open the home page. Not sure why the COOKIE is not getting deleted… I set the maxAge to 0 and set other params like path/domain but no use. Please advice.

    • Hi,

      you have to close the browser, this is the best recommended way in
      case of single sign on.
      Reason is of the configurations, these server used to manage client tokens.

      Also are you using site minder or CAS?

  2. HI everybody,
    i have a question. I want to parse via Jaxb unmarshaller a ServletInputstream.
    But the stream contains overhead, so the jaxb unmarshaller can not recognize the stream content as xml.
    What should I do?
    Thanks for help.

  3. Have long used the ByteArrayInputStream for unmarshalling, but for a still unkown reason in a recent project this started creating “Content is not allowed in trailing section” exceptions. When I changed to simply use a StringReader this fixed itself. One difference is that in this project I do not have @XmlRootElement annotations and have to unmarshal to a JAXBElement, but I don’t see how that makes a difference.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s