/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONArray using the JsonML transform.<p> * * Each XML tag is represented as * a JSONArray in which the first element is the tag name. If the tag has * attributes, then the second element will be JSONObject containing the * name/value pairs. If the tag contains children, then strings and * JSONArrays will represent the child tags. * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.<p> * * @param string the source string * @return a JSONArray containing the structured data from the XML string. * @throws JSONException if something goes wrong */ public static JSONArray toJSONArray(String string) throws JSONException { return toJSONArray(new XMLTokener(string)); }
/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONArray using the JsonML transform.<p> * * Each XML tag is represented as * a JSONArray in which the first element is the tag name. If the tag has * attributes, then the second element will be JSONObject containing the * name/value pairs. If the tag contains children, then strings and * JSONArrays will represent the child tags. * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.<p> * * @param string the source string * @return a JSONArray containing the structured data from the XML string. * @throws JSONException if something goes wrong */ public static JSONArray toJSONArray(String string) throws JSONException { return toJSONArray(new XMLTokener(string)); }
/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject.<p> * * Some information may be lost in this transformation * because JSON is a data format and XML is a document format. XML uses * elements, attributes, and content text, while JSON uses unordered * collections of name/value pairs and arrays of values. JSON does not * does not like to distinguish between elements and attributes.<p> * * Sequences of similar elements are represented as JSONArrays. Content * text may be placed in a "content" member. Comments, prologs, DTDs, and * <code><[ [ ]]></code> are ignored.<p> * * @param string the source string * @return a JSONObject containing the structured data from the XML string * @throws JSONException if something goes wrong */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); XMLTokener x = new XMLTokener(string); while (x.more() && x.skipPast("<")) { parse(x, o, null); } return o; }
/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject.<p> * * Some information may be lost in this transformation * because JSON is a data format and XML is a document format. XML uses * elements, attributes, and content text, while JSON uses unordered * collections of name/value pairs and arrays of values. JSON does not * does not like to distinguish between elements and attributes.<p> * * Sequences of similar elements are represented as JSONArrays. Content * text may be placed in a "content" member. Comments, prologs, DTDs, and * <code><[ [ ]]></code> are ignored.<p> * * @param string the source string * @return a JSONObject containing the structured data from the XML string * @throws JSONException if something goes wrong */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); XMLTokener x = new XMLTokener(string); while (x.more() && x.skipPast("<")) { parse(x, o, null); } return o; }