1

I want to convert an XML document to a JSON object without the XML attributes. For example:

XML:

<root id="120">
<child1 id="21">val1<child1>
<child2 id="22">val2<child2>
</root>

Desired JSON:

{
    "root":{
        "child1": val1,
        "child2": val2
    }
}

Converting the XML to JSONObject, then removing each attributes seems like complex and less efficient way. Is there any recommended library or technique to achieve the same in less code and efficient way in Java?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3243499
  • 2,953
  • 6
  • 33
  • 75
  • I'd consider `xslt` tbh, don't know how efficient that is though. I'd assume most efficient Java solution would be using a SAX parser. – daniu Mar 28 '18 at 09:15
  • [possibly a duplicate](https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java) – thar45 Mar 28 '18 at 09:16
  • @gks: Mate, please read the question carefully. I am not asking how to convert xml to json, rather, how to convert xml to json WITHOUT attributes and in an EFFICIENT way. Please check your reference link and let me know how it is a duplicate. Thank you so much, – user3243499 Mar 28 '18 at 09:20
  • @daniu: Just a small side question: do you know how to use the APG parser for ABNF grammars in JAVA? – user3243499 Mar 28 '18 at 09:22

1 Answers1

0

The simplest way to convert XML to JSON would be...

  • First create/map java object from XML using JAXB.
  • Then create JSONObject from the converted java object using any library like org.json, or gson etc.
Harsh Mehta
  • 571
  • 3
  • 16