1

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!

Community
  • 1
  • 1
DA--
  • 723
  • 1
  • 8
  • 20

1 Answers1

1

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks for the suggestion! I've been trying and searching quite a bit but I can't figure out which package implements these functions. Or is it implemented in the standard Java libraries? To be honest: I'm quite new to Java... – DA-- Aug 05 '16 at 13:05
  • XPath 3.1 is implemented in recent versions of Saxon, and by a few other libraries such as BaseX. – Michael Kay Aug 05 '16 at 17:21
  • Note, as far as I can see Badgerfish concentrates on ensuring that for every XML document, there is a defined JSON representation. The XPath conversion functions concentrate on ensuring that for every JSON document, there is a defined XML representation. This means that with the Badgerfish conventions, there will be JSON documents that don't translate to XML (e.g. `{"name with space":true()}`, while with the XPath convention, there will be XML documents that can't be converted to JSON. So the two are suited for rather different purposes. – Michael Kay Aug 05 '16 at 17:29
  • I was not aware of XPath and XSLT. I learned how to use it, and for me this works much better than the badgerfish conversion. – DA-- Aug 08 '16 at 14:21