Convert XML to JSON using org.apache.commons.json.utils.XML toJson - changes empty element to "true"
I am trying to convert xml string to Json in Java. Here's some sample code:
import org.apache.commons.json.utils.XML;
String test = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><a><b>val1</b><d/></a>";
InputStream is = new ByteArrayInputStream(test.getBytes());
String jsonString = XML.toJson(is);
Result:
{"a": {"b": "zal1", "r": true}}
I don't understand why d is set to true?
There is also a way to get this result:
{"a": {"b": "value1", "d": ""}}
+3
source to share
1 answer
I did a little investigation, the org.apache.apache.wink.json4j.utils.XML.toJson method uses SAXParser, I was unable to debug (it warned me due to missing line number attributes (is this because of the decompiler?), So or otherwise), but I think this is true for an empty tag.
Then I debugged apache.sling.commons.xml.XML.toJSONObject, it has its own XMLTokenizer. In my estimation, because of the empty SAXParser tag, this is true.
+1
source to share