xml to json transformation Spring/Java - java

I am looking for "transforming" (not plain-converting) XML(one data format) to JSON (to other/diff data format). Is there any tool/lib/framework to achieve this in Spring or Java?
Example
Input:
<root>
<element1>abc</element1>
<element2>xyz</element2>
.....
</root>
Output
{
"regionCode":"abc",
"regionName":"xyz"
...............
}
Thanks
Bharath

Please consider searching for your question. Have you considered camel? http://camel.apache.org/xmljson.html

What is the difference between "transform" and "convert"? Regardless, Jackson can read/write both JSON and XML (using the XML extension). You'll have to deserialize your XML into objects before serializing them as JSON but that's not a bad thing - it would be difficult to go directly from XML to JSON except in the most simple cases (JSON does not have attributes, for example).

Related

Is there a Java XML to JSON library to produce output JSON with no property where input XML has xsi:nil="true"

Is there a Java XML to JSON library to produce output JSON with no property where input XML has xsi:nil="true"?
XML input example:
<root>
<ele1>Has content</ele2>
<ele2 xsi:nil="true" />
</root>
Expected JSON output
{
"root":{
"ele1":"Has content"
}
}
My input is XML not a POJO. I want to see if there is a library that avoids e.g. using JAXB/Jackson.
Using json.org, I currently get e.g.:
{
"root":{
"ele1":"Has content",
"ele2":[
"xsi:nil":true
]
}
}
I would prefer not to have null values in the JSON, but the properties not to be there. I.e. not:
{
"root":{
"ele1":"Has content",
"ele2":null
}
}
Thanks,
There is no XML-to-JSON conversion library that will always give you exactly what you want. Generally, I would recommend doing an XML-to-XML pre-conversion using XSLT before you do the XML-to-JSON conversion - in this case the pre-conversion would eliminate the nilled elements.
If you want complete control over the generated JSON, try the mechanism in XSLT 3.0, where you first convert the XML to generic <map>, <array>, <string> and <number> elements, and then press the button to convert these to JSON syntax.

Convert one json format to another in java

I am looking for a utility which converts one json format to another by respecting at the conversion definitions from a preferably xml file. Is there any library doing something like this in java ?
For example source json is:
{"name":"aa","surname":"bb","accounts":[{"accountid":10,"balance":100}]}
target json is :
{"owner":"aa-bb","accounts":[{"accountid":10,"balance":100}]}
sample config xml :
t.owner = s.name.concat("-").concat(surname)
t.accounts = t.accounts
Ps:Please dont post solutions for this example, it is just for giving an idea, there will be quite different scenarios in mapping.
Is this what u need?
Open input file.
Read / parse JSON from file using a JSON library.
Convert in-memory data structure to new structure.
Open output file
Unparse in-memory data structure to file using JSON library.

Converting a java object to both XML and JSON not producing consistent output

I am trying to convert a java object within an ArrayList into both XML and JSON strings and then send it over to ActiveMQ server.
For converting to XML I am using XStream API and for converting to JSON I am using Google GSON API. I want to have consistent output after conversion in both the cases which I don't see here. One of them is showing the tag and the entire package name whereas the other one completely omits both.
Is there any standard conversion API available that I can use which actually converts the input object to a consistent output format for both XML and JSON?
Input Object (ArrayList containing some POJO):
==================
eventsLst = [PageViewEvent{pageName=Home Page, pageType=Home}]
XML Conversion (Using XStream):
====================================
<list>
<com.istore.event.model.PageViewEvent>
<pageName>Home Page</pageName>
<pageType>Home</pageType>
</com.istore.event.model.PageViewEvent>
</list>
JSON Conversion (Using GSON):
========================================
[{"pageName":"Home Page","pageType":"Home"}]

parse XML to POJO using XStream

I have an xml file which I want to read and parse it into POJO.
I am using XStream for this.
I am not able to send the file as an input to the code for parsing(file is on my local drive).
How to read the xml file and parse it using fromXML() method ?
I would be gratefull if someone can give example for sending xml file as input and parsing it to POJO and printing it on the screen
Thanks...
Based on the provided comments, I can better understand the problem you are facing. I believe the answer you are looking for is implicit collections. Check out the XStream tutorial on aliasing: http://x-stream.github.io/alias-tutorial.html .

Best approach to serialize XML to stream with Java?

We serialize/deserialize XML using XStream... and just got an OutOfMemory exception.
Firstly I don't understand why we're getting the error as we have 500MB allocated to the server.
Question is - what changes should we make to stay out of trouble? We want to ensure this implementation scales.
Currently we have ~60K objects, each ~50 bytes. We load the 60K POJO's in memory, and serialize them to a String which we send to a web service using HttpClient. When receiving, we get the entire String, then convert to POJO's. The XML/object hierarchy is like:
<root>
<meta>
<date>10/10/2009</date>
<type>abc</type>
</meta>
<data>
<field>x</field>
</data>
[thousands of <data>]
</root>
I gather the best approach is to not store the POJO's in memory and not write the contents to a single String. Instead we should write the individual <data> POJO's to a stream. XStream supports this but seems like the <meta> element wouldn't be supported. Data would need to be in form:
<root>
<data>
<field>x</field>
</data>
[thousands of <data>]
</root>
So what approach is easiest to stream the entire tree?
You definitely want to avoid serializing your POJOs into a humongous String and then writing that String out. Use the XStream APIs to serialize the POJOs directly to your OutputStream. I ran into the same situation earlier this year when I found that I was generating 200-300Mb XML documents and getting OutOfMemoryErrors. It was very easy to make the switch.
And ditto of course for the reading side. Don't read the XML into a String and ask XStream to deserialize from that String: deserialize directly from the InputStream.
You mention a second issue regarding not being able to serialize the <meta> element and the <data> elements. I don't think this is an XStream problem or limitation as I routinely serialize much more complex structures on the order of:
<myobject>
<item>foo</item>
<anotheritem>foo</anotheritem>
<alist>
<alistitem>
<value1>v1</value1>
<value2>v2</value2>
<value3>v3</value3>
...
</alistitem>
...
<alistitem>
<value1>v1</value1>
<value2>v2</value2>
<value3>v3</value3>
...
</alistitem>
</alist>
<anotherlist>
<anotherlistitem>
<valA>A</valA>
<valB>B</valB>
<valC>C</valC>
...
</anotherlistitem>
...
</anotherlist>
</myobject>
I've successfully serialized and deserialized nested lists too.
Not sure what the problem is here...you've found your answer on that webpage.
The example code on the link you provided suggests:
Writer someWriter = new FileWriter("filename.xml");
ObjectOutputStream out = xstream.createObjectOutputStream(someWriter, "root");
out.writeObject(dataObject);
// iterate over your objects...
out.close();
and for reading nearly identical but with Reader for Writer and Input for Output:
Reader someReader = new FileReader("filename.xml");
ObjectInputStream in = xstream.createObjectInputStream(someReader);
DataObject foo = (DataObject)in.readObject();
// do some stuff here while there's more objects...
in.close();
I'd suggest using tools like Visual VM or Eclipse Memory Analyzer to make sure you don't have a memory leak/problem.
Also, how do you know each object is 50 bytes? That doesn't sound likely.
Use XMLStreamWriter (or XStream) to serialize it, you can write whatever you want on it. If you have the option of getting the input stream instead of the entire string, use a SAXParser, it is event based and, although the implementation maybe a little bit clumsy, you will be able to read any XML that is thrown at you, even if it the XML is huge (I have parse 2GB+ more XML files with SAXParser).
Just as a side note, you should send the binary data and not the string to a XML parser. XML parsers will read the encoding of the byte array that is going to come next through the xml tag in the beginning of the XML sequence:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
A string is encoded in something already. It's better practice to let the XML parse the original stream before you create a String with it.

Categories