Convert XML to JSON using org.apache.commons.json.utils.XML toJson - Changes empty element to "true"

1.6k views Asked by At

I'm trying to to convert an xml string to Json in Java. Here is a 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);

The result is:

{"a":{"b":"val1","d":true}}

I don't understand why d's value is set to true ?

Also is there any way to get this result:

{"a":{"b":"val1","d":""}}

1

There are 1 answers

0
İlker Korkut On

I did a little investigation, org.apache.apache.wink.json4j.utils.XML.toJson method uses SAXParser , i couldn't debugged(it warned me due to missing line number attributes(is it because of decompiler?), anyway) it, but i think it makes true for empty tag.

Then I debugged apache.sling.commons.xml.XML.toJSONObject it has own XMLTokenizer. In my estimation because of SAXParser empty tag comes true.