@@ -24,6 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
2424SOFTWARE.
2525*/
2626
27+ import java .io .Reader ;
28+ import java .io .StringReader ;
2729import java .util .Iterator ;
2830
2931/**
@@ -470,6 +472,56 @@ public static JSONObject toJSONObject(String string) throws JSONException {
470472 return toJSONObject (string , false );
471473 }
472474
475+ /**
476+ * Convert a well-formed (but not necessarily valid) XML into a
477+ * JSONObject. Some information may be lost in this transformation because
478+ * JSON is a data format and XML is a document format. XML uses elements,
479+ * attributes, and content text, while JSON uses unordered collections of
480+ * name/value pairs and arrays of values. JSON does not does not like to
481+ * distinguish between elements and attributes. Sequences of similar
482+ * elements are represented as JSONArrays. Content text may be placed in a
483+ * "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
484+ * are ignored.
485+ *
486+ * @param reader The XML source reader.
487+ * @return A JSONObject containing the structured data from the XML string.
488+ * @throws JSONException Thrown if there is an errors while parsing the string
489+ */
490+ public static JSONObject toJSONObject (Reader reader ) throws JSONException {
491+ return toJSONObject (reader , false );
492+ }
493+
494+ /**
495+ * Convert a well-formed (but not necessarily valid) XML into a
496+ * JSONObject. Some information may be lost in this transformation because
497+ * JSON is a data format and XML is a document format. XML uses elements,
498+ * attributes, and content text, while JSON uses unordered collections of
499+ * name/value pairs and arrays of values. JSON does not does not like to
500+ * distinguish between elements and attributes. Sequences of similar
501+ * elements are represented as JSONArrays. Content text may be placed in a
502+ * "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
503+ * are ignored.
504+ *
505+ * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
506+ * numbers but will instead be the exact value as seen in the XML document.
507+ *
508+ * @param reader The XML source reader.
509+ * @param keepStrings If true, then values will not be coerced into boolean
510+ * or numeric values and will instead be left as strings
511+ * @return A JSONObject containing the structured data from the XML string.
512+ * @throws JSONException Thrown if there is an errors while parsing the string
513+ */
514+ public static JSONObject toJSONObject (Reader reader , boolean keepStrings ) throws JSONException {
515+ JSONObject jo = new JSONObject ();
516+ XMLTokener x = new XMLTokener (reader );
517+ while (x .more ()) {
518+ x .skipPast ("<" );
519+ if (x .more ()) {
520+ parse (x , jo , null , keepStrings );
521+ }
522+ }
523+ return jo ;
524+ }
473525
474526 /**
475527 * Convert a well-formed (but not necessarily valid) XML string into a
@@ -493,16 +545,9 @@ public static JSONObject toJSONObject(String string) throws JSONException {
493545 * @throws JSONException Thrown if there is an errors while parsing the string
494546 */
495547 public static JSONObject toJSONObject (String string , boolean keepStrings ) throws JSONException {
496- JSONObject jo = new JSONObject ();
497- XMLTokener x = new XMLTokener (string );
498- while (x .more ()) {
499- x .skipPast ("<" );
500- if (x .more ()) {
501- parse (x , jo , null , keepStrings );
502- }
503- }
504- return jo ;
548+ return toJSONObject (new StringReader (string ), keepStrings );
505549 }
550+
506551 /**
507552 * Convert a JSONObject into a well-formed, element-normal XML string.
508553 *
0 commit comments