Skip to content

Commit 42e0944

Browse files
yaccobJohn J. Aylward
authored andcommitted
Brings in changes from PR stleary#70 to be updated to HEAD
1 parent 04181fb commit 42e0944

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

JSONML.java

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ private static Object parse(
5050
XMLTokener x,
5151
boolean arrayForm,
5252
JSONArray ja
53+
) throws JSONException {
54+
return parse(x, arrayForm, ja, false);
55+
}
56+
57+
/**
58+
* Parse XML values and store them in a JSONArray.
59+
* @param x The XMLTokener containing the source string.
60+
* @param arrayForm true if array form, false if object form.
61+
* @param ja The JSONArray that is containing the current tag or null
62+
* if we are at the outermost level.
63+
* @param keepStrings Don't type-convert text nodes and attibute values
64+
* @return A JSONArray if the value is the outermost tag, otherwise null.
65+
* @throws JSONException
66+
*/
67+
private static Object parse(
68+
XMLTokener x,
69+
boolean arrayForm,
70+
JSONArray ja,
71+
boolean keepStrings
5372
) throws JSONException {
5473
String attribute;
5574
char c;
@@ -174,7 +193,7 @@ private static Object parse(
174193
if (!(token instanceof String)) {
175194
throw x.syntaxError("Missing value");
176195
}
177-
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
196+
newjo.accumulate(attribute, keepStrings ? token :JSONObject.stringToValue((String)token));
178197
token = null;
179198
} else {
180199
newjo.accumulate(attribute, "");
@@ -204,7 +223,7 @@ private static Object parse(
204223
if (token != XML.GT) {
205224
throw x.syntaxError("Misshaped tag");
206225
}
207-
closeTag = (String)parse(x, arrayForm, newja);
226+
closeTag = (String)parse(x, arrayForm, newja, keepStrings);
208227
if (closeTag != null) {
209228
if (!closeTag.equals(tagName)) {
210229
throw x.syntaxError("Mismatched '" + tagName +
@@ -227,7 +246,7 @@ private static Object parse(
227246
} else {
228247
if (ja != null) {
229248
ja.put(token instanceof String
230-
? JSONObject.stringToValue((String)token)
249+
? keepStrings ? token :JSONObject.stringToValue((String)token)
231250
: token);
232251
}
233252
}
@@ -252,6 +271,46 @@ public static JSONArray toJSONArray(String string) throws JSONException {
252271
}
253272

254273

274+
/**
275+
* Convert a well-formed (but not necessarily valid) XML string into a
276+
* JSONArray using the JsonML transform. Each XML tag is represented as
277+
* a JSONArray in which the first element is the tag name. If the tag has
278+
* attributes, then the second element will be JSONObject containing the
279+
* name/value pairs. If the tag contains children, then strings and
280+
* JSONArrays will represent the child tags.
281+
* As opposed to toJSONArray this method does not attempt to convert
282+
* any text node or attribute value to any type
283+
* but just leaves it as a string.
284+
* Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
285+
* @param string The source string.
286+
* @return A JSONArray containing the structured data from the XML string.
287+
* @throws JSONException
288+
*/
289+
public static JSONArray toJsonML(String string) throws JSONException {
290+
return toJsonML(new XMLTokener(string));
291+
}
292+
293+
294+
/**
295+
* Convert a well-formed (but not necessarily valid) XML string into a
296+
* JSONArray using the JsonML transform. Each XML tag is represented as
297+
* a JSONArray in which the first element is the tag name. If the tag has
298+
* attributes, then the second element will be JSONObject containing the
299+
* name/value pairs. If the tag contains children, then strings and
300+
* JSONArrays will represent the child content and tags.
301+
* As opposed to toJSONArray this method does not attempt to convert
302+
* any text node or attribute value to any type
303+
* but just leaves it as a string.
304+
* Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
305+
* @param x An XMLTokener.
306+
* @return A JSONArray containing the structured data from the XML string.
307+
* @throws JSONException
308+
*/
309+
public static JSONArray toJsonML(XMLTokener x) throws JSONException {
310+
return (JSONArray)parse(x, true, null, true);
311+
}
312+
313+
255314
/**
256315
* Convert a well-formed (but not necessarily valid) XML string into a
257316
* JSONArray using the JsonML transform. Each XML tag is represented as

0 commit comments

Comments
 (0)