@@ -24,6 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
24
24
SOFTWARE.
25
25
*/
26
26
27
+ import java .io .Reader ;
28
+ import java .io .StringReader ;
27
29
import java .util .Iterator ;
28
30
29
31
/**
@@ -470,6 +472,56 @@ public static JSONObject toJSONObject(String string) throws JSONException {
470
472
return toJSONObject (string , false );
471
473
}
472
474
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
+ }
473
525
474
526
/**
475
527
* Convert a well-formed (but not necessarily valid) XML string into a
@@ -493,16 +545,9 @@ public static JSONObject toJSONObject(String string) throws JSONException {
493
545
* @throws JSONException Thrown if there is an errors while parsing the string
494
546
*/
495
547
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 );
505
549
}
550
+
506
551
/**
507
552
* Convert a JSONObject into a well-formed, element-normal XML string.
508
553
*
0 commit comments