How do I convert an XML string to JSON?

View: New views
2 Messages — Rating Filter:   Alert me  

How do I convert an XML string to JSON?

by Alex Quezada :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Since I need to pass my objects through JAXB annotations before  
turning into JSON, I'm first marshalling to XML.  Now I need to go  
from an XML string to a JSON string.  I'm pretty sure that's possible,  
since I found an example to do the opposite (JSON string to XML  
string), but no luck so far.  Any ideas?

Thanks!

Alex

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: How do I convert an XML string to JSON?

by Jörg Schaible-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Alex,

Alex Quezada wrote:

> Hello,
>
> Since I need to pass my objects through JAXB annotations before
> turning into JSON, I'm first marshalling to XML.  Now I need to go
> from an XML string to a JSON string.  I'm pretty sure that's possible,
> since I found an example to do the opposite (JSON string to XML
> string), but no luck so far.  Any ideas?

String toJSON(String xml) {
  StringWriter writer = new StringWriter();
  new HierarchicalStreamCopier().copy(
    new DomReader(xml), new JsonWriter(writer));
  return writer.toString();
}

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email