I Have a SOAP web services which takes XML as input and send XML as response. Now I have a JSON object which is the same as XML request. How can I convert JSON Object to XML so that I can send the XML request to my SOAP web service. I have the WSDL file. In other terms, I wanted to provide a extra layer of REST on top of SOAP
Asked
Active
Viewed 5,094 times
0
-
which language you are using, provide that information in question or tag the question with language. – Red Boy Jul 04 '18 at 06:11
-
You basically need a JSON<-> XML conversion prior to posting to your web service. See this question: https://stackoverflow.com/questions/83405/xml-parser-for-javascript/26957382#26957382 Disclaimer: I'm the author of [Jsonix](https://github.com/highsource/jsonix). – lexicore Jul 04 '18 at 11:46
1 Answers
1
A JSON object can never be "the same as" an XML document, because the data models are different. It may hold the same information, but the problem is that there is no definitive mapping from JSON to XML that is guaranteed to produce the XML representation that you are after.
There are a number of libraries available that do JSON to XML conversion: give them a try, but you will probably have to "tweak" the XML to get it into the required form. That can always be done using XSLT, of course.
If you use XSLT 3.0 then you can do the JSON to XML conversion and subsequent processing ("tweaking") in a single step.

Michael Kay
- 156,231
- 11
- 92
- 164
-
You can have basically the same data model with different representation (XML or JSON). An annotated Java POJO can be converted to XML (with JAXB) or JSON (with, say, Jackson) at the same time, but it's the same model underneath. – lexicore Jul 04 '18 at 11:49
-
JSON and XML most definitely are NOT the same data model. If they were the same data model then there would be a reversible mapping from one to the other that is lossless in both directions, and that is demonstrably not the case. To add to the problem, both XML and JSON are fuzzy about defining their data models, e.g. the status of whitespace in XML or duplicate keys in JSON. – Michael Kay Jul 04 '18 at 19:04
-
With certain limitations, a losless reversible mapping from one to the other is possible. I wrote a [tool](https://github.com/highsource/jsonix) which does this pretty well. – lexicore Jul 04 '18 at 19:37
-
Unless I'm mistaken, your tool provides a way to define custom mappings for specific vocabularies, which is only needed because there is no sufficiently general vocabulary-independent mapping from one model to the other. For example, any mapping that translates element/attribute names to JSON map keys is not completely reversible because not all map keys are valid XML names. – Michael Kay Jul 04 '18 at 20:59
-
Yes, this is more or less correct. This is very similar to JAXB, just for JS. Still, this is a losless reversible mapping which pretty much does the job. – lexicore Jul 04 '18 at 21:22
-
I have POJO Java that define my XML. I just need to convert that JSON to XML using that POJO – realcodes Jul 05 '18 at 19:20