Convert XML object to Json using velocity template - java

I have a use-case where input data is in xml format. It contains multiple fields where we are interested in reading only few fields.
Based on the different use-case, separate velocity templates would have to written to convert xml to json data.
I could not find a better guide on how to write velocity template to convert XML to json. Can someone please help us how to write velocity for conversion from XML to JSON.
Thanks in advance!

First convert your XML into a DTO, and then DTO into Json. You should split your architecture in more than just one layer. The data access layer will handle the WS call and the controller will know how to answer the REST call. Velocity is just a template engine, not a converting tool. You should check Jackson or Gson.

Related

How to convert Badgerfish style JSON to XML in Java?

My Java application takes XML input, and parses it using the simpleframework. I want it to accept JSON as well, therefore I want to convert the JSON to XML.
Tags and attributes are important, therefore I use the Badgerfish convention.
This works well in Python with xmljson, but I can't find a decent package to do this. GSON doesn't seem to have a Badgerfish implementation. This topic doesn't provide any tag/attribute retaining packages, the topic is a bit old as well.
Which Java packages can do the conversion from JSON to XML while putting tags/attributes at the right place?
Suggestions for alternative methods than Badgerfish are welcome as well...
Many thanks in advance!
You can use the XPath 3.1 json-to-xml() function, and then do an XSLT transformation on the generated XML to get it into the format you require.

How to get a simple JSON out of a MongoDb-Document (Java)

I work with MongoDb and Jersey in Java. I like to return the Documents I read out of the DB directly as JSON to my AngularJS client. If I call toJson() on a Document I get this BSON representation. That means that a Long for example is represented as an object with a key-value-pair "$numberLong". I don't like this type information in my JSON.
Is there any way to get a simple JSON out of this Document, without converting it to a Java-Object and back? What are the best practices to work with this Documents? Would you include that type information?

JSON To JSON Transformation | Template Engine for Java

I have a requirement to transform an incoming JSON to an output JSON. For this I am looking for a solution that can work based on templates. What I have in my mind is a solution on lines of XSLT transformation that allows converting an XML to a desired output format (XML, HTML, Text) defined by the style sheet.
One option(or rather a workaround) to use XSLT is to convert JSON to XML that is:
input JSON -> XML -> transform -> output JSON
This approach would have a performance overhead of converting JSON to XML and this would become prominent as the size of incoming object increases.
I found a Node/client layer solution that transforms JSON based on the rules specified in a template. More details about can be found [here][1]. However, I was not able to find any solution that works for a java based application.
Any thoughts/help in terms of solution/frameworks to resolve this would be really helpful.
Thanks.
You could try JOLT, advertised as a JSON to JSON transformation library written in Java.
Or you can search this thread for other libraries and tools which can transform JSON.
The new XSLT 3.0 draft also includes support for JSON as input and output format. Saxon has already started an implementation and seems to support for the JSON part.
You could try JSLT, which is a transform language where you write the fixed part of the output in JSON syntax, then insert expressions to compute the values you want to insert in the template. It's quite similar to how XSLT and XPath work together.
It's implemented in Java on top of Jackson.
For simple transformations you can use jmom library.
For a complex transformation you can use template framework like freemarker.
And convert json data to Map/List form using json library so it can be used by template framework.

Unable to unmarshal strange XML format using Java and JAXB

I need to retrieve financial data using the Open Financial Exchange (OFX) protocol. In order to do this, I am using JAXB to marshal an object tree into an XML string that specifies data request parameters, and then I am sending this XML string to a bank's server. The bank then responds with an XML string containing the requested data, which I unmarshal into an object tree using JAXB. For the first couple of banks I tried, I received the data back in well-formed XML that conformed to the published OFX schema, and I was able to unmarshal it easily using JAXB.
However, when I requested data from Citigroup, they sent me back the following:
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<DTSERVER>20150513180826.000
<LANGUAGE>ENG
<FI>
<ORG>Citigroup
<FID>24909
</FI>
</SONRS>
</SIGNONMSGSRSV1>
</OFX>
Note that this is an abbreviated form of the actual output, but it is enough to illustrate the problem. The problem is that I cannot figure out how to use JAXB to unmarshal this content. It is not well-formed XML because (1) it doesn't have an XML header, (2) the custom processing instructions (the first nine lines above) are not enclosed in <?...?> tags, and (3) most importantly, the simpleTypes have only opening tags but no closing tags.
I have searched all over for an answer to this and found a similar XML-ish format in a couple of places, and one of those places indicated that this may even be a valid format for sending XML over the web. But I haven't found any information that can help me unmarshal it or parse it.
Does anyone have any suggestions? I am usually pretty resourceful when it comes to these types of problems (hence why this is my first question on here), but this one has me stumped. Thanks in advance for any help you can provide.
Your basic problem is that the input you show here is not XML, it's SGML (see DATA:OFXSGML). You will have to preprocess it to make it acceptable to an XML parser. The kind of preprocessing you have to do will be application specific, as there's no general mechanism to deal well with that. If you have the SGML DTD, you might be able to get a product such as omnimark to "mostly" fix it up.
Well , maybe you need to handle this bank services in some other manner, for example when you receive data from this bank maybe read the Stream and maybe try to undetify the beggining of tag and then the end of (read line by line link)the rest of the stream ..free will . After that the string that remains is the XML that you need , so pass it through your already implemented JAXB code.

how to convert http request to JSON object in javaee5

I want to convert request xml to JSON string. which framework is better to use? jettison, jackson, json-org,... and also how can I do this?
Any idea?
thanks
Afsaneh
If the goal is to go straight from XML to JSON, without any structure transformations, data changes, or inspections, then XStream or even the JSON in Java reference implementation at org.json can get the job done rather simply. In similar fashion, XSLT options are available, including XSLTJSON and xml2json-xslt.
If complicated interrogation and/or manipulation of the data and resulting JSON are in order, then Jackson combined with the jackson-xml-databind extension provide for a feature-rich option that also has excellent performance. (Performance comparisons of some JSON and XML serialization APIs are available at https://github.com/eishay/jvm-serializers/wiki.)

Categories

Resources