user994165
user994165

Reputation: 9512

Convert an XML String to a JSON Object Object

I'm trying to convert an XML string to a JSON object. I used net.sf.json, but it strips away the root element.

XMLSerializer xmlSerializer = new XMLSerializer();
net.sf.json.JSON netsfJSON = xmlSerializer.read(xml);

I also tried org.json.XML

JSONObject jsonObject=XML.toJSONObject(xml);

but it doesn't differentiate the attributes from the elements. Is there anything else I can use or are there some settings in the two libraries above?

Upvotes: 1

Views: 1979

Answers (1)

user2055588
user2055588

Reputation: 11

After createing the XMLSerializer object you need to set the forceTopLevelObject property to true due to the fact that the default setting is false.

XMLSerializer xmlSerializer = new XMLSerializer();

xmlSerializer.setForceTopLevelObject(true);

Hope this helps.

Carol

Upvotes: 1

Related Questions