org.json - XML
Die XML-Klasse bietet statische Methoden zum Konvertieren eines XML-Textes in ein JSONObject und umgekehrt.
Die folgenden Methoden werden im Beispiel behandelt.
toJSONObject(String) - Konvertiert ein XML in ein JSONArray-Objekt.
toString(JSONObject) - Gibt ein XML von einem JSONObject-Objekt.
Beispiel
import org.json.JSONObject;
import org.json.XML;
public class JSONDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", "Robert");
jsonObject.put("ID", 1);
jsonObject.put("Fees", new Double(1000.21));
jsonObject.put("Active", new Boolean(true));
jsonObject.put("Details", JSONObject.NULL);
//Convert a JSONObject to XML
String xmlText = XML.toString(jsonObject);
System.out.println(xmlText);
//Convert an XML to JSONObject
System.out.println(XML.toJSONObject(xmlText));
}
}
Ausgabe
<Active>true</Active><Details>null</Details><ID>1</ID><Fees>1000.21</Fees><Name>Robert</Name>
{"Active":true,"Details":null,"ID":1,"Fees":1000.21,"Name":"Robert"}