/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject.<p> * * 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.<p> * * 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.<p> * * @param string the source string * @return a JSONObject containing the structured data from the XML string * @throws JSONException if something goes wrong */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); XMLTokener x = new XMLTokener(string); while (x.more() && x.skipPast("<")) { parse(x, o, null); } return o; }
StringBuffer sb; do { c = next(); } while (Character.isWhitespace(c)); if (c == 0) { for (;;) { if ((c == '<') || (c == 0)) { back(); return sb.toString().trim(); sb.append(nextEntity(c)); } else { sb.append(c); c = next();
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; default:
/** * Return the next ENTITY. These entities are translated to Characters: * <code>& ' > < "</code>.<p> * * @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 + ";"; }
t = x.nextContent(); if (t == XML.LT) { t = x.nextToken(); if (t instanceof Character) { c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); x.back(); } else if (c == '[') { t = x.nextToken(); if (t.equals("CDATA") && x.next() == '[') { x.nextCDATA(); } else { throw x.syntaxError("Expected 'CDATA['"); t = x.nextMeta(); if (t == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (t == XML.LT) { i += 1; x.skipPast("?>"); } else if (t == XML.SLASH) { t = x.nextToken(); if (ja == null) {
StringBuffer 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 StringBuffer(); 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.<p> * * 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.<p> * * @param string the source string * @return a JSONArray containing the structured data from the XML string. * @throws JSONException if something goes wrong */ public static JSONArray toJSONArray(String string) throws JSONException { return toJSONArray(new XMLTokener(string)); }
t = x.nextContent(); if (t == XML.LT) { t = x.nextToken(); if (t instanceof Character) { c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); x.back(); } else if (c == '[') { t = x.nextToken(); if (t.equals("CDATA") && (x.next() == '[')) { x.nextCDATA(); } else { throw x.syntaxError("Expected 'CDATA['"); t = x.nextMeta(); if (t == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (t == XML.LT) { i += 1; x.skipPast("?>"); } else if (t == XML.SLASH) { t = x.nextToken(); if (ja == null) {
StringBuffer 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 StringBuffer(); 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 '[':
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; default:
/** * Get the text in the CDATA block.<p> * * @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 (c == 0) { 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(); } } }
/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONArray using the JsonML transform.<p> * * 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.<p> * * @param string the source string * @return a JSONArray containing the structured data from the XML string. * @throws JSONException if something goes wrong */ public static JSONArray toJSONArray(String string) throws JSONException { return toJSONArray(new XMLTokener(string)); }
t = x.nextToken(); c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); return false; x.back(); } else if (c == '[') { t = x.nextToken(); if (t.equals("CDATA")) { if (x.next() == '[') { s = x.nextCDATA(); if (s.length() > 0) { context.accumulate("content", s); throw x.syntaxError("Expected 'CDATA['"); t = x.nextMeta(); if (t == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (t == LT) { i += 1; x.skipPast("?>"); return false; } else if (t == SLASH) {
/** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject.<p> * * 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.<p> * * 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.<p> * * @param string the source string * @return a JSONObject containing the structured data from the XML string * @throws JSONException if something goes wrong */ public static JSONObject toJSONObject(String string) throws JSONException { JSONObject o = new JSONObject(); XMLTokener x = new XMLTokener(string); while (x.more() && x.skipPast("<")) { parse(x, o, null); } return o; }
StringBuffer sb; do { c = next(); } while (Character.isWhitespace(c)); if (c == 0) { for (;;) { if (c == '<' || 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>.<p> * * @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 + ";"; }
t = x.nextToken(); c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); return false; x.back(); } else if (c == '[') { t = x.nextToken(); if (t.equals("CDATA")) { if (x.next() == '[') { s = x.nextCDATA(); if (s.length() > 0) { context.accumulate("content", s); throw x.syntaxError("Expected 'CDATA['"); t = x.nextMeta(); if (t == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (t == LT) { i += 1; x.skipPast("?>"); return false; } else if (t == SLASH) {
/** * Get the text in the CDATA block.<p> * * @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 (c == 0) { 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(); } } }