Question

I am having xml containing some html content in some node like

<detail>
&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;
</detail>

while conversion from xml to Json by json or json-lib, by

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer; 
.....
JSONObject jsonObject = XML.toJSONObject(xml.toString());
......

xml is string containing xml content

I should get json string as

{"detail":"&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;"}

But output is something like

{"detail":{"P":"Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision."}}

which is not proper.

Can anyone tell how can I resolve this issue?

Was it helpful?

Solution

Try using the library from www.json.org instead of net.sf.json.JSON

import org.json.JSONObject;
import org.json.XML;

String xmlString = "<detail>&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;</detail>";
System.out.println("Initial XML : " + xmlString);
JSONObject jsonObj = (XML.toJSONObject(xmlString));
System.out.println("Converted JSON : " + jsonObj.toString());
System.out.println("Back to converted XML : " + XML.toString(jsonObj));

You'll get this result that looks better to my mind :

Initial XML : <detail>&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;</detail>
Converted JSON : {"detail":"<P>Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn't the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.<\/P>"}
Back to converted XML : <detail>&lt;P&gt;Students should avoid purchasing their textbooks at the first store that they browse. Instead, they should investigate the alternatives offered by other online booksellers. Price isn&apos;t the only factor to consider when making an online purchase. Students should also factor in shipping costs and delivery time when making their buying decision.&lt;/P&gt;</detail>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top