/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject. 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. 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. * * All values are converted as strings, for 1, 01, 29.0 will not be coerced to * numbers but will instead be the exact value as seen in the XML document. * * @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 JSONObject containing the structured data from the XML string. * @throws JSONException Thrown if there is an errors while parsing the string */ public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException { JSONObject jo = new JSONObject(); XMLTokener x = new XMLTokener(string); while (x.more() && x.skipPast("<")) { parse(x, jo, null, keepStrings); } return jo; } /**
if (!x.more()) { throw x.syntaxError("Bad XML"); token = x.nextContent(); if (token == XML.LT) { token = x.nextToken(); if (token instanceof Character) { if (token == XML.SLASH) { token = x.nextToken(); if (!(token instanceof String)) { throw new JSONException( if (x.nextToken() != XML.GT) { throw x.syntaxError("Misshaped close tag"); c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); } else { x.back(); token = x.nextToken(); if (token.equals("CDATA") && x.next() == '[') { if (ja != null) { ja.put(x.nextCDATA()); throw x.syntaxError("Expected 'CDATA['");
StringBuilder sb; do { c = next(); } while (Character.isWhitespace(c)); if (c == 0) { back(); return sb.toString().trim(); sb.append(nextEntity(c)); } else { sb.append(c); c = next();
/** * Return the next entity. These entities are translated to Characters: * <code>& ' > < "</code>. * @param ampersand An ampersand character. * @return A Character or an entity String if the entity is not recognized. * @throws JSONException If missing ';' in XML entity. */ public Object nextEntity(char ampersand) throws JSONException { StringBuilder sb = new StringBuilder(); for (;;) { char c = next(); if (Character.isLetterOrDigit(c) || c == '#') { sb.append(Character.toLowerCase(c)); } else if (c == ';') { break; } else { throw syntaxError("Missing ';' in XML entity: &" + sb); } } String string = sb.toString(); return unescapeEntity(string); }
/** * Get the text in the CDATA block. * @return The string up to the <code>]]></code>. * @throws JSONException If the <code>]]></code> is not found. */ public String nextCDATA() throws JSONException { char c; int i; StringBuilder sb = new StringBuilder(); while (more()) { c = next(); sb.append(c); i = sb.length() - 3; if (i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') { sb.setLength(i); return sb.toString(); } } throw syntaxError("Unclosed CDATA"); }
StringBuilder sb; do { c = next(); } while (Character.isWhitespace(c)); switch (c) { case 0: throw syntaxError("Misshaped element"); case '<': throw syntaxError("Misplaced '<'"); case '>': return XML.GT; sb = new StringBuilder(); for (;;) { c = next(); if (c == 0) { throw syntaxError("Unterminated string"); sb.append(nextEntity(c)); } else { sb.append(c); for (;;) { sb.append(c); c = next(); if (Character.isWhitespace(c)) { return sb.toString(); case '[':
/** * 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); }
/** * Return the next entity. These entities are translated to Characters: * <code>&amp; &apos; &gt; &lt; &quot;</code>. * @param a An ampersand character. * @return A Character or an entity String if the entity is not recognized. * @throws JSONException If missing ';' in XML entity. */ public Object nextEntity(char a) throws JSONException { StringBuffer sb = new StringBuffer(); for (;;) { char c = next(); if (Character.isLetterOrDigit(c) || c == '#') { sb.append(Character.toLowerCase(c)); } else if (c == ';') { break; } else { throw syntaxError("Missing ';' in XML entity: &" + sb); } } String s = sb.toString(); Object e = entity.get(s); return e != null ? e : a + s + ";"; }
char q; do { c = next(); } while (Character.isWhitespace(c)); switch (c) { case 0: throw syntaxError("Misshaped meta tag"); case '<': return XML.LT; q = c; for (;;) { c = next(); if (c == 0) { throw syntaxError("Unterminated string"); c = next(); if (Character.isWhitespace(c)) { return Boolean.TRUE; case '"': case '\'': back(); return Boolean.TRUE;
/** * Get the text in the CDATA block. * @return The string up to the <code>]]></code>. * @throws JSONException If the <code>]]></code> is not found. */ public String nextCDATA() throws JSONException { char c; int i; StringBuffer sb = new StringBuffer(); for (;;) { c = next(); if (end()) { throw syntaxError("Unclosed CDATA"); } sb.append(c); i = sb.length() - 3; if (i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') { sb.setLength(i); return sb.toString(); } } }
sb.append(nextEntity(c)); } else { sb.append(c);
StringBuilder sb; do { c = next(); } while (Character.isWhitespace(c)); switch (c) { case 0: throw syntaxError("Misshaped element"); case '<': throw syntaxError("Misplaced '<'"); case '>': return XML.GT; sb = new StringBuilder(); for (;;) { c = next(); if (c == 0) { throw syntaxError("Unterminated string"); sb.append(nextEntity(c)); } else { sb.append(c); for (;;) { sb.append(c); c = next(); if (Character.isWhitespace(c)) { return sb.toString(); case '[':
/** * Get the text in the CDATA block. * @return The string up to the <code>]]></code>. * @throws JSONException If the <code>]]></code> is not found. */ public String nextCDATA() throws JSONException { char c; int i; StringBuilder sb = new StringBuilder(); while (more()) { c = next(); sb.append(c); i = sb.length() - 3; if (i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') { sb.setLength(i); return sb.toString(); } } throw syntaxError("Unclosed CDATA"); }
/** * 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); }
/** * Return the next entity. These entities are translated to Characters: * <code>& ' > < "</code>. * @param ampersand An ampersand character. * @return A Character or an entity String if the entity is not recognized. * @throws JSONException If missing ';' in XML entity. */ public Object nextEntity(char ampersand) throws JSONException { StringBuilder sb = new StringBuilder(); for (;;) { char c = next(); if (Character.isLetterOrDigit(c) || c == '#') { sb.append(Character.toLowerCase(c)); } else if (c == ';') { break; } else { throw syntaxError("Missing ';' in XML entity: &" + sb); } } String string = sb.toString(); Object object = entity.get(string); return object != null ? object : ampersand + string + ";"; }
char q; do { c = next(); } while (Character.isWhitespace(c)); switch (c) { case 0: throw syntaxError("Misshaped meta tag"); case '<': return XML.LT; q = c; for (;;) { c = next(); if (c == 0) { throw syntaxError("Unterminated string"); c = next(); if (Character.isWhitespace(c)) { return Boolean.TRUE; case '"': case '\'': back(); return Boolean.TRUE;
/** * Return the next entity. These entities are translated to Characters: * <code>& ' > < "</code>. * @param ampersand An ampersand character. * @return A Character or an entity String if the entity is not recognized. * @throws JSONException If missing ';' in XML entity. */ public Object nextEntity(@SuppressWarnings("unused") char ampersand) throws JSONException { StringBuilder sb = new StringBuilder(); for (;;) { char c = next(); if (Character.isLetterOrDigit(c) || c == '#') { sb.append(Character.toLowerCase(c)); } else if (c == ';') { break; } else { throw syntaxError("Missing ';' in XML entity: &" + sb); } } String string = sb.toString(); return unescapeEntity(string); }
token = x.nextToken(); c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); return false; x.back(); } else if (c == '[') { token = x.nextToken(); if ("CDATA".equals(token)) { if (x.next() == '[') { string = x.nextCDATA(); if (string.length() > 0) { context.accumulate("content", string); throw x.syntaxError("Expected 'CDATA['"); token = x.nextMeta(); if (token == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (token == LT) { i += 1; x.skipPast("?>"); return false; } else if (token == SLASH) {