You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Magnus Palmér <ma...@gmail.com> on 2011/03/26 19:04:20 UTC

Simple convert XML to JSON?

Hi,

I am trying to read a xml-file via the file component and I split and filter
the result and then change the dataformat from XML to JSON before sending it
out with the file component again.
I just can't seem to find a solution using Google or in my Camel In Action
book.
(My real world xml is totally different, but using a simple  xml without
namespace for prototyping).

Input xml:
*<?xml version="1.0"?>
<root><a><id>1</id></a><a><id>2</id></a></root>

*My route:
*from("file:src/data/simple?noop=true").tracing()
   .split(xpath("/root/a")).filter(xpath("/a/id[.='1']"))
   .convertBodyTo(String.class).marshal()
   .json(JsonLibrary.Jackson)
   .to("file:target/messages?fileName=id1.json");*

Wanted output.json:
{a:{id:1}}

Result using Jackson:
"<a><id>1</id></a>"

(without the convertBodyTo: "<?xml version=\"1.0\"
encoding=\"UTF-16\"?>\n<a><id>1</id></a>")

Result using Xstream instead (and adding .convertBodyTo(String.class) before
marshalling)
{"string":"<a><id>1<\/id><\/a>"}

Seems as if I am missing some fundamental understanding of how things work?
How can I get my XML to be recognized as XML (which obviously the split and
xpath do)?

-- 
Brgds, Magnus Palmér
+46736845680

Re: Simple convert XML to JSON?

Posted by Fernando Ribeiro <we...@fernandoribeiro.eti.br>.
You might want to take a look at
https://github.com/upictec/JsonTypeConverter and
https://github.com/upictec/JsonDataFormat, which are surely going to welcome
your feedback and/or participation too. Thanks.

On Sun, Mar 27, 2011 at 9:07 PM, magnuspalmer
<ma...@gmail.com>wrote:

> I decided to model the domain object described by the schema and using Jaxb
> annotations it worked fine to marshal it to Json the way I wanted.
>
> Still I am curious about how to easily convert a simple xml file to json so
> that one xml element or attribute gets a one to one relationship with json.
> Right usage of type converters?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Simple-convert-XML-to-JSON-tp4265729p4266826.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Simple convert XML to JSON?

Posted by magnuspalmer <ma...@gmail.com>.
I decided to model the domain object described by the schema and using Jaxb
annotations it worked fine to marshal it to Json the way I wanted.

Still I am curious about how to easily convert a simple xml file to json so
that one xml element or attribute gets a one to one relationship with json.
Right usage of type converters?



--
View this message in context: http://camel.465427.n5.nabble.com/Simple-convert-XML-to-JSON-tp4265729p4266826.html
Sent from the Camel - Users mailing list archive at Nabble.com.