Package org.json

Class XML


  • public class XML
    extends java.lang.Object
    This provides static methods to convert an XML text into a JSONObject, and to covert a JSONObject into an XML text.
    Version:
    2016-08-10
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.Character AMP
      The Character '&'.
      static java.lang.Character APOS
      The Character '''.
      static java.lang.Character BANG
      The Character '!'.
      static java.lang.Character EQ
      The Character '='.
      static java.lang.Character GT
      The Character
      static java.lang.Character LT
      The Character '<'.
      static java.lang.String NULL_ATTR
      Null attribute name
      static java.lang.Character QUEST
      The Character '?'.
      static java.lang.Character QUOT
      The Character '"'.
      static java.lang.Character SLASH
      The Character '/'.
      static java.lang.String TYPE_ATTR
      Represents the XML attribute name for specifying type information.
    • Constructor Summary

      Constructors 
      Constructor Description
      XML()
      Constructs a new XML object.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Iterable<java.lang.Integer> codePointIterator​(java.lang.String string)
      Creates an iterator for navigating Code Points in a string instead of characters.
      static java.lang.String escape​(java.lang.String string)
      Replace special characters with XML escapes:
      private static java.lang.String indent​(int indent)
      Return a String consisting of a number of space characters specified by indent
      private static boolean isDecimalNotation​(java.lang.String val)
      direct copy of JSONObject.isDecimalNotation(String) to maintain Android support.
      private static boolean isStringAllWhiteSpace​(java.lang.String s)  
      private static boolean mustEscape​(int cp)  
      static void noSpace​(java.lang.String string)
      Throw an exception if the string contains whitespace.
      private static boolean parse​(XMLTokener x, JSONObject context, java.lang.String name, XMLParserConfiguration config, int currentNestingDepth)
      Scan the content following the named tag, attaching it to the context.
      private static void removeEmpty​(JSONObject jsonObject, XMLParserConfiguration config)
      This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName and contains whitespace as this is caused by whitespace between tags.
      private static java.lang.Number stringToNumber​(java.lang.String val)
      direct copy of JSONObject.stringToNumber(String) to maintain Android support.
      static java.lang.Object stringToValue​(java.lang.String string)
      This method is the same as JSONObject.stringToValue(String).
      static java.lang.Object stringToValue​(java.lang.String string, XMLXsiTypeConverter<?> typeConverter)
      This method tries to convert the given string value to the target object
      static JSONObject toJSONObject​(java.io.Reader reader)
      Convert a well-formed (but not necessarily valid) XML into a JSONObject.
      static JSONObject toJSONObject​(java.io.Reader reader, boolean keepStrings)
      Convert a well-formed (but not necessarily valid) XML into a JSONObject.
      static JSONObject toJSONObject​(java.io.Reader reader, XMLParserConfiguration config)
      Convert a well-formed (but not necessarily valid) XML into a JSONObject.
      static JSONObject toJSONObject​(java.lang.String string)
      Convert a well-formed (but not necessarily valid) XML string into a JSONObject.
      static JSONObject toJSONObject​(java.lang.String string, boolean keepStrings)
      Convert a well-formed (but not necessarily valid) XML string into a JSONObject.
      static JSONObject toJSONObject​(java.lang.String string, XMLParserConfiguration config)
      Convert a well-formed (but not necessarily valid) XML string into a JSONObject.
      static java.lang.String toString​(java.lang.Object object)
      Convert a JSONObject into a well-formed, element-normal XML string.
      static java.lang.String toString​(java.lang.Object object, int indentFactor)
      Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
      static java.lang.String toString​(java.lang.Object object, java.lang.String tagName)
      Convert a JSONObject into a well-formed, element-normal XML string.
      static java.lang.String toString​(java.lang.Object object, java.lang.String tagName, int indentFactor)
      Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
      static java.lang.String toString​(java.lang.Object object, java.lang.String tagName, XMLParserConfiguration config)
      Convert a JSONObject into a well-formed, element-normal XML string.
      static java.lang.String toString​(java.lang.Object object, java.lang.String tagName, XMLParserConfiguration config, int indentFactor)
      Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
      private static java.lang.String toString​(java.lang.Object object, java.lang.String tagName, XMLParserConfiguration config, int indentFactor, int indent)
      Convert a JSONObject into a well-formed, element-normal XML string, either pretty print or single-lined depending on indent factor.
      static java.lang.String unescape​(java.lang.String string)
      Removes XML escapes from the string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • AMP

        public static final java.lang.Character AMP
        The Character '&'.
      • APOS

        public static final java.lang.Character APOS
        The Character '''.
      • BANG

        public static final java.lang.Character BANG
        The Character '!'.
      • EQ

        public static final java.lang.Character EQ
        The Character '='.
      • GT

        public static final java.lang.Character GT
        The Character
        '>'. 
      • LT

        public static final java.lang.Character LT
        The Character '<'.
      • QUEST

        public static final java.lang.Character QUEST
        The Character '?'.
      • QUOT

        public static final java.lang.Character QUOT
        The Character '"'.
      • SLASH

        public static final java.lang.Character SLASH
        The Character '/'.
      • NULL_ATTR

        public static final java.lang.String NULL_ATTR
        Null attribute name
        See Also:
        Constant Field Values
      • TYPE_ATTR

        public static final java.lang.String TYPE_ATTR
        Represents the XML attribute name for specifying type information.
        See Also:
        Constant Field Values
    • Constructor Detail

      • XML

        public XML()
        Constructs a new XML object.
    • Method Detail

      • codePointIterator

        private static java.lang.Iterable<java.lang.Integer> codePointIterator​(java.lang.String string)
        Creates an iterator for navigating Code Points in a string instead of characters. Once Java7 support is dropped, this can be replaced with string.codePoints() which is available in Java8 and above.
        See Also:
        http://stackoverflow.com/a/21791059/6030888
      • escape

        public static java.lang.String escape​(java.lang.String string)
        Replace special characters with XML escapes:
        
         &amp; (ampersand) is replaced by &amp;amp;
         &lt; (less than) is replaced by &amp;lt;
         &gt; (greater than) is replaced by &amp;gt;
         &quot; (double quote) is replaced by &amp;quot;
         &apos; (single quote / apostrophe) is replaced by &amp;apos;
         
        Parameters:
        string - The string to be escaped.
        Returns:
        The escaped string.
      • mustEscape

        private static boolean mustEscape​(int cp)
        Parameters:
        cp - code point to test
        Returns:
        true if the code point is not valid for an XML
      • unescape

        public static java.lang.String unescape​(java.lang.String string)
        Removes XML escapes from the string.
        Parameters:
        string - string to remove escapes from
        Returns:
        string with converted entities
      • noSpace

        public static void noSpace​(java.lang.String string)
                            throws JSONException
        Throw an exception if the string contains whitespace. Whitespace is not allowed in tagNames and attributes.
        Parameters:
        string - A string.
        Throws:
        JSONException - Thrown if the string contains whitespace or is empty.
      • parse

        private static boolean parse​(XMLTokener x,
                                     JSONObject context,
                                     java.lang.String name,
                                     XMLParserConfiguration config,
                                     int currentNestingDepth)
                              throws JSONException
        Scan the content following the named tag, attaching it to the context.
        Parameters:
        x - The XMLTokener containing the source string.
        context - The JSONObject that will include the new material.
        name - The tag name.
        config - The XML parser configuration.
        currentNestingDepth - The current nesting depth.
        Returns:
        true if the close tag is processed.
        Throws:
        JSONException - Thrown if any parsing error occurs.
      • removeEmpty

        private static void removeEmpty​(JSONObject jsonObject,
                                        XMLParserConfiguration config)
        This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled.
        Parameters:
        jsonObject - JSONObject which may require deletion
        config - The XMLParserConfiguration which includes the cDataTagName
      • isStringAllWhiteSpace

        private static boolean isStringAllWhiteSpace​(java.lang.String s)
      • stringToNumber

        private static java.lang.Number stringToNumber​(java.lang.String val)
                                                throws java.lang.NumberFormatException
        direct copy of JSONObject.stringToNumber(String) to maintain Android support.
        Throws:
        java.lang.NumberFormatException
      • stringToValue

        public static java.lang.Object stringToValue​(java.lang.String string,
                                                     XMLXsiTypeConverter<?> typeConverter)
        This method tries to convert the given string value to the target object
        Parameters:
        string - String to convert
        typeConverter - value converter to convert string to integer, boolean e.t.c
        Returns:
        JSON value of this string or the string
      • stringToValue

        public static java.lang.Object stringToValue​(java.lang.String string)
        This method is the same as JSONObject.stringToValue(String).
        Parameters:
        string - String to convert
        Returns:
        JSON value of this string or the string
      • toJSONObject

        public static JSONObject toJSONObject​(java.lang.String string)
                                       throws JSONException
        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
        
         &lt;[ [ ]]>
        are ignored.
        Parameters:
        string - The source string.
        Returns:
        A JSONObject containing the structured data from the XML string.
        Throws:
        JSONException - Thrown if there is an errors while parsing the string
      • toJSONObject

        public static JSONObject toJSONObject​(java.io.Reader reader)
                                       throws JSONException
        Convert a well-formed (but not necessarily valid) XML 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
        
         &lt;[ [ ]]>
        are ignored.
        Parameters:
        reader - The XML source reader.
        Returns:
        A JSONObject containing the structured data from the XML string.
        Throws:
        JSONException - Thrown if there is an errors while parsing the string
      • toJSONObject

        public static JSONObject toJSONObject​(java.io.Reader reader,
                                              boolean keepStrings)
                                       throws JSONException
        Convert a well-formed (but not necessarily valid) XML 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
        
         &lt;[ [ ]]>
        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.
        Parameters:
        reader - The XML source reader.
        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 if there is an errors while parsing the string
      • toJSONObject

        public static JSONObject toJSONObject​(java.io.Reader reader,
                                              XMLParserConfiguration config)
                                       throws JSONException
        Convert a well-formed (but not necessarily valid) XML 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
        
         &lt;[ [ ]]>
        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.
        Parameters:
        reader - The XML source reader.
        config - Configuration options for the parser
        Returns:
        A JSONObject containing the structured data from the XML string.
        Throws:
        JSONException - Thrown if there is an errors while parsing the string
      • toJSONObject

        public static JSONObject toJSONObject​(java.lang.String string,
                                              boolean keepStrings)
                                       throws JSONException
        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
        
         &lt;[ [ ]]>
        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.
        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 JSONObject containing the structured data from the XML string.
        Throws:
        JSONException - Thrown if there is an errors while parsing the string
      • toJSONObject

        public static JSONObject toJSONObject​(java.lang.String string,
                                              XMLParserConfiguration config)
                                       throws JSONException
        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
        
         &lt;[ [ ]]>
        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.
        Parameters:
        string - The source string.
        config - Configuration options for the parser.
        Returns:
        A JSONObject containing the structured data from the XML string.
        Throws:
        JSONException - Thrown if there is an errors while parsing the string
      • toString

        public static java.lang.String toString​(java.lang.Object object)
                                         throws JSONException
        Convert a JSONObject into a well-formed, element-normal XML string.
        Parameters:
        object - A JSONObject.
        Returns:
        A string.
        Throws:
        JSONException - Thrown if there is an error parsing the string
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.String tagName)
        Convert a JSONObject into a well-formed, element-normal XML string.
        Parameters:
        object - A JSONObject.
        tagName - The optional name of the enclosing tag.
        Returns:
        A string.
        Throws:
        JSONException - Thrown if there is an error parsing the string
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.String tagName,
                                                XMLParserConfiguration config)
                                         throws JSONException
        Convert a JSONObject into a well-formed, element-normal XML string.
        Parameters:
        object - A JSONObject.
        tagName - The optional name of the enclosing tag.
        config - Configuration that can control output to XML.
        Returns:
        A string.
        Throws:
        JSONException - Thrown if there is an error parsing the string
      • toString

        private static java.lang.String toString​(java.lang.Object object,
                                                 java.lang.String tagName,
                                                 XMLParserConfiguration config,
                                                 int indentFactor,
                                                 int indent)
                                          throws JSONException
        Convert a JSONObject into a well-formed, element-normal XML string, either pretty print or single-lined depending on indent factor.
        Parameters:
        object - A JSONObject.
        tagName - The optional name of the enclosing tag.
        config - Configuration that can control output to XML.
        indentFactor - The number of spaces to add to each level of indentation.
        indent - The current ident level in spaces.
        Returns:
        Throws:
        JSONException
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                int indentFactor)
        Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
        Parameters:
        object - A JSONObject.
        indentFactor - The number of spaces to add to each level of indentation.
        Returns:
        A string.
        Throws:
        JSONException - Thrown if there is an error parsing the string
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.String tagName,
                                                int indentFactor)
        Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
        Parameters:
        object - A JSONObject.
        tagName - The optional name of the enclosing tag.
        indentFactor - The number of spaces to add to each level of indentation.
        Returns:
        A string.
        Throws:
        JSONException - Thrown if there is an error parsing the string
      • toString

        public static java.lang.String toString​(java.lang.Object object,
                                                java.lang.String tagName,
                                                XMLParserConfiguration config,
                                                int indentFactor)
                                         throws JSONException
        Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
        Parameters:
        object - A JSONObject.
        tagName - The optional name of the enclosing tag.
        config - Configuration that can control output to XML.
        indentFactor - The number of spaces to add to each level of indentation.
        Returns:
        A string.
        Throws:
        JSONException - Thrown if there is an error parsing the string
      • indent

        private static final java.lang.String indent​(int indent)
        Return a String consisting of a number of space characters specified by indent
        Parameters:
        indent - The number of spaces to be appended to the String.
        Returns: