+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONArray using the JsonML transform. 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.
+ *
+ * @param string
+ * The source string.
+ * @return A JSONArray containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONArray
+ */
+ public static JSONArray toJSONArray(String string) throws JSONException
+ {
+ return (JSONArray) parse(new XMLTokener(string), true, null, false);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONArray using the JsonML transform. 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. As opposed to toJSONArray this method does
+ * not attempt to convert any text node or attribute value to any type but
+ * just leaves it as a string. Comments, prologs, DTDs, and
+ * <code><[ [ ]]></code> are ignored.
+ *
+ * @param string
+ * The source string.
+ * @param keepStrings
+ * If true, then values will not be coerced into boolean or numeric
+ * values and will instead be left as strings
+ * @return A JSONArray containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONArray
+ */
+ public static JSONArray toJSONArray(String string, boolean keepStrings)
+ throws JSONException
+ {
+ return (JSONArray) parse(new XMLTokener(string), true, null,
+ keepStrings);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONArray using the JsonML transform. 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 content and tags. As opposed to toJSONArray this
+ * method does not attempt to convert any text node or attribute value to any
+ * type but just leaves it as a string. Comments, prologs, DTDs, and
+ * <code><[ [ ]]></code> are ignored.
+ *
+ * @param x
+ * An XMLTokener.
+ * @param keepStrings
+ * If true, then values will not be coerced into boolean or numeric
+ * values and will instead be left as strings
+ * @return A JSONArray containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONArray
+ */
+ public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings)
+ throws JSONException
+ {
+ return (JSONArray) parse(x, true, null, keepStrings);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONArray using the JsonML transform. 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 content and tags. Comments, prologs, DTDs, and
+ * <code><[ [ ]]></code> are ignored.
+ *
+ * @param x
+ * An XMLTokener.
+ * @return A JSONArray containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONArray
+ */
+ public static JSONArray toJSONArray(XMLTokener x) throws JSONException
+ {
+ return (JSONArray) parse(x, true, null, false);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject using the JsonML transform. Each XML tag is represented as a
+ * JSONObject with a "tagName" property. If the tag has attributes, then the
+ * attributes will be in the JSONObject as properties. If the tag contains
+ * children, the object will have a "childNodes" property which will be an
+ * array of strings and JsonML JSONObjects.
+ *
+ * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
+ *
+ * @param string
+ * The XML source text.
+ * @return A JSONObject containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONObject
+ */
+ public static JSONObject toJSONObject(String string) throws JSONException
+ {
+ return (JSONObject) parse(new XMLTokener(string), false, null, false);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject using the JsonML transform. Each XML tag is represented as a
+ * JSONObject with a "tagName" property. If the tag has attributes, then the
+ * attributes will be in the JSONObject as properties. If the tag contains
+ * children, the object will have a "childNodes" property which will be an
+ * array of strings and JsonML JSONObjects.
+ *
+ * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
+ *
+ * @param string
+ * The XML source text.
+ * @param keepStrings
+ * If true, then values will not be coerced into boolean or numeric
+ * values and will instead be left as strings
+ * @return A JSONObject containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONObject
+ */
+ public static JSONObject toJSONObject(String string, boolean keepStrings)
+ throws JSONException
+ {
+ return (JSONObject) parse(new XMLTokener(string), false, null,
+ keepStrings);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject using the JsonML transform. Each XML tag is represented as a
+ * JSONObject with a "tagName" property. If the tag has attributes, then the
+ * attributes will be in the JSONObject as properties. If the tag contains
+ * children, the object will have a "childNodes" property which will be an
+ * array of strings and JsonML JSONObjects.
+ *
+ * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
+ *
+ * @param x
+ * An XMLTokener of the XML source text.
+ * @return A JSONObject containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONObject
+ */
+ public static JSONObject toJSONObject(XMLTokener x) throws JSONException
+ {
+ return (JSONObject) parse(x, false, null, false);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject using the JsonML transform. Each XML tag is represented as a
+ * JSONObject with a "tagName" property. If the tag has attributes, then the
+ * attributes will be in the JSONObject as properties. If the tag contains
+ * children, the object will have a "childNodes" property which will be an
+ * array of strings and JsonML JSONObjects.
+ *
+ * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
+ *
+ * @param x
+ * An XMLTokener of the XML source text.
+ * @param keepStrings
+ * If true, then values will not be coerced into boolean or numeric
+ * values and will instead be left as strings
+ * @return A JSONObject containing the structured data from the XML string.
+ * @throws JSONException
+ * Thrown on error converting to a JSONObject
+ */
+ public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings)
+ throws JSONException
+ {
+ return (JSONObject) parse(x, false, null, keepStrings);
+ }
+
+ /**
+ * Reverse the JSONML transformation, making an XML text from a JSONArray.
+ *
+ * @param ja
+ * A JSONArray.
+ * @return An XML string.
+ * @throws JSONException
+ * Thrown on error converting to a string
+ */
+ public static String toString(JSONArray ja) throws JSONException
+ {
+ int i;
+ JSONObject jo;
+ int length;
+ Object object;
+ StringBuilder sb = new StringBuilder();
+ String tagName;
+
+ // Emit <tagName
+
+ tagName = ja.getString(0);
+ XML.noSpace(tagName);
+ tagName = XML.escape(tagName);
+ sb.append('<');
+ sb.append(tagName);
+
+ object = ja.opt(1);
+ if (object instanceof JSONObject)
+ {
+ i = 2;
+ jo = (JSONObject) object;
+
+ // Emit the attributes
+
+ // Don't use the new entrySet API to maintain Android support
+ for (final String key : jo.keySet())
+ {
+ final Object value = jo.opt(key);
+ XML.noSpace(key);
+ if (value != null)
+ {
+ sb.append(' ');
+ sb.append(XML.escape(key));
+ sb.append('=');
+ sb.append('"');
+ sb.append(XML.escape(value.toString()));
+ sb.append('"');
+ }
+ }