Package org.json
Class JSONML
java.lang.Object
org.json.JSONML
This provides static methods to convert an XML text into a JSONArray or
JSONObject, and to covert a JSONArray or JSONObject into an XML text using
the JsonML transform.
- Version:
- 2016-01-30
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static Object
parse
(XMLTokener x, boolean arrayForm, JSONArray ja, boolean keepStrings, int currentNestingDepth) Parse XML values and store them in a JSONArray.private static Object
parse
(XMLTokener x, boolean arrayForm, JSONArray ja, JSONMLParserConfiguration config, int currentNestingDepth) Parse XML values and store them in a JSONArray.static JSONArray
toJSONArray
(String string) Convert a well-formed (but not necessarily valid) XML string into a JSONArray using the JsonML transform.static JSONArray
toJSONArray
(String string, boolean keepStrings) Convert a well-formed (but not necessarily valid) XML string into a JSONArray using the JsonML transform.static JSONArray
toJSONArray
(String string, JSONMLParserConfiguration config) Convert a well-formed (but not necessarily valid) XML string into a JSONArray using the JsonML transform.static JSONArray
Convert a well-formed (but not necessarily valid) XML string into a JSONArray using the JsonML transform.static JSONArray
toJSONArray
(XMLTokener x, boolean keepStrings) Convert a well-formed (but not necessarily valid) XML string into a JSONArray using the JsonML transform.static JSONArray
toJSONArray
(XMLTokener x, JSONMLParserConfiguration config) Convert a well-formed (but not necessarily valid) XML string into a JSONArray using the JsonML transform.static JSONObject
toJSONObject
(String string) Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform.static JSONObject
toJSONObject
(String string, boolean keepStrings) Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform.static JSONObject
toJSONObject
(String string, JSONMLParserConfiguration config) Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform.static JSONObject
Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform.static JSONObject
toJSONObject
(XMLTokener x, boolean keepStrings) Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform.static JSONObject
toJSONObject
(XMLTokener x, JSONMLParserConfiguration config) Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform.static String
Reverse the JSONML transformation, making an XML text from a JSONArray.static String
toString
(JSONObject jo) Reverse the JSONML transformation, making an XML text from a JSONObject.
-
Constructor Details
-
JSONML
public JSONML()Constructs a new JSONML object.
-
-
Method Details
-
parse
private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja, boolean keepStrings, int currentNestingDepth) throws JSONException Parse XML values and store them in a JSONArray.- Parameters:
x
- The XMLTokener containing the source string.arrayForm
- true if array form, false if object form.ja
- The JSONArray that is containing the current tag or null if we are at the outermost level.keepStrings
- Don't type-convert text nodes and attribute values- Returns:
- A JSONArray if the value is the outermost tag, otherwise null.
- Throws:
JSONException
- if a parsing error occurs
-
parse
private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja, JSONMLParserConfiguration config, int currentNestingDepth) throws JSONException Parse XML values and store them in a JSONArray.- Parameters:
x
- The XMLTokener containing the source string.arrayForm
- true if array form, false if object form.ja
- The JSONArray that is containing the current tag or null if we are at the outermost level.config
- The parser configuration: JSONMLParserConfiguration.ORIGINAL is the default behaviour; JSONMLParserConfiguration.KEEP_STRINGS means Don't type-convert text nodes and attribute values.- Returns:
- A JSONArray if the value is the outermost tag, otherwise null.
- Throws:
JSONException
- if a parsing error occurs
-
toJSONArray
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
are ignored.<[ [ ]]>
- Parameters:
string
- The source string.- Returns:
- A JSONArray containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONArray
-
toJSONArray
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. As opposed to toJSONArray this method does not attempt to convert any text node or attribute value to any type but just leaves it as a string. Comments, prologs, DTDs, and
are ignored.<[ [ ]]>
- Parameters:
string
- The source string.keepStrings
- If true, then values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONArray containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONArray
-
toJSONArray
public static JSONArray toJSONArray(String string, JSONMLParserConfiguration config) throws JSONException 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. As opposed to toJSONArray this method does not attempt to convert any text node or attribute value to any type but just leaves it as a string. Comments, prologs, DTDs, and
are ignored.<[ [ ]]>
- Parameters:
string
- The source string.config
- The parser configuration: JSONMLParserConfiguration.ORIGINAL is the default behaviour; JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONArray containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONArray
-
toJSONArray
public static JSONArray toJSONArray(XMLTokener x, JSONMLParserConfiguration config) throws JSONException 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 content and tags. As opposed to toJSONArray this method does not attempt to convert any text node or attribute value to any type but just leaves it as a string. Comments, prologs, DTDs, and
are ignored.<[ [ ]]>
- Parameters:
x
- An XMLTokener.config
- The parser configuration: JSONMLParserConfiguration.ORIGINAL is the default behaviour; JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONArray containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONArray
-
toJSONArray
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 content and tags. As opposed to toJSONArray this method does not attempt to convert any text node or attribute value to any type but just leaves it as a string. Comments, prologs, DTDs, and
are ignored.<[ [ ]]>
- Parameters:
x
- An XMLTokener.keepStrings
- If true, then values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONArray containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONArray
-
toJSONArray
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 content and tags. Comments, prologs, DTDs, and
are ignored.<[ [ ]]>
- Parameters:
x
- An XMLTokener.- Returns:
- A JSONArray containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONArray
-
toJSONObject
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
are ignored.<[ [ ]]>
- Parameters:
string
- The XML source text.- Returns:
- A JSONObject containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONObject
-
toJSONObject
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
are ignored.<[ [ ]]>
- Parameters:
string
- The XML source text.keepStrings
- If true, then values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONObject containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONObject
-
toJSONObject
public static JSONObject toJSONObject(String string, JSONMLParserConfiguration config) throws JSONException 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
are ignored.<[ [ ]]>
- Parameters:
string
- The XML source text.config
- The parser configuration: JSONMLParserConfiguration.ORIGINAL is the default behaviour; JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONObject containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONObject
-
toJSONObject
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
are ignored.<[ [ ]]>
- Parameters:
x
- An XMLTokener of the XML source text.- Returns:
- A JSONObject containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONObject
-
toJSONObject
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
are ignored.<[ [ ]]>
- Parameters:
x
- An XMLTokener of the XML source text.keepStrings
- If true, then values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONObject containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONObject
-
toJSONObject
public static JSONObject toJSONObject(XMLTokener x, JSONMLParserConfiguration config) throws JSONException 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
are ignored.<[ [ ]]>
- Parameters:
x
- An XMLTokener of the XML source text.config
- The parser configuration: JSONMLParserConfiguration.ORIGINAL is the default behaviour; JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean or numeric values and will instead be left as strings- Returns:
- A JSONObject containing the structured data from the XML string.
- Throws:
JSONException
- Thrown on error converting to a JSONObject
-
toString
Reverse the JSONML transformation, making an XML text from a JSONArray.- Parameters:
ja
- A JSONArray.- Returns:
- An XML string.
- Throws:
JSONException
- Thrown on error converting to a string
-
toString
Reverse the JSONML transformation, making an XML text from a JSONObject. The JSONObject must contain a "tagName" property. If it has children, then it must have a "childNodes" property containing an array of objects. The other properties are attributes with string values.- Parameters:
jo
- A JSONObject.- Returns:
- An XML string.
- Throws:
JSONException
- Thrown on error converting to a string
-