org.apache.wink.json4j.utils
Class XML

java.lang.Object
  extended by org.apache.wink.json4j.utils.XML

public class XML
extends java.lang.Object

This class is a static helper for various ways of converting an XML document/InputStream into a JSON stream or String and vice-versa. For example, the XML document:

<getValuesReturn return="true"> <attribute attrValue="value"/> <String>First item</String> <String>Second item</String> <String>Third item</String> <TextTag>Text!</TextTag> <EmptyTag/> <TagWithAttrs attr1="value1" attr2="value2" attr3="value3"/> <TagWithAttrsAndText attr1="value1" attr2="value2" attr3="value3">Text!</TagWithAttrsAndText> </getValuesReturn>
in JSON (in non-compact form) becomes
{ "getValuesReturn" : { "return" : "true", "TextTag" : "Text!", "String" : [ "First item", "Second item", "Third item" ], "TagWithAttrsAndText" : { "content" : "Text!", "attr3" : "value3", "attr2" : "value2", "attr1" : "value1" } , "EmptyTag" : true, "attribute" : { "attrValue" : "value" } , "TagWithAttrs" : { "attr3" : "value3", "attr2" : "value2", "attr1" : "value1" } } }


Constructor Summary
XML()
           
 
Method Summary
static java.lang.String toJson(java.io.File xmlFile)
          Method to take an XML file and return a String of the JSON format.
static java.lang.String toJson(java.io.File xmlFile, boolean verbose)
          Method to take an XML file and return a String of the JSON format.
static java.lang.String toJson(java.io.InputStream xmlStream)
          Method to take an input stream to an XML document and return a String of the JSON format.
static java.lang.String toJson(java.io.InputStream xmlStream, boolean verbose)
          Method to take an input stream to an XML document and return a String of the JSON format.
static void toJson(java.io.InputStream XMLStream, java.io.OutputStream JSONStream)
          Method to do the transform from an XML input stream to a JSON stream.
static void toJson(java.io.InputStream XMLStream, java.io.OutputStream JSONStream, boolean verbose)
          Method to do the transform from an XML input stream to a JSON stream.
static java.lang.String toXml(java.io.File jsonFile)
          Method to take an JSON file and return a String of the XML format.
static java.lang.String toXml(java.io.File jsonFile, boolean verbose)
          Method to take a JSON file and return a String of the XML format.
static java.lang.String toXml(java.io.InputStream JSONStream)
          Method to take an input stream to an JSON document and return a String of the XML format.
static java.lang.String toXml(java.io.InputStream JSONStream, boolean verbose)
          Method to take an input stream to an JSON document and return a String of the XML format.
static void toXml(java.io.InputStream JSONStream, java.io.OutputStream XMLStream)
          Method to do the transform from an JSON input stream to a XML stream.
static void toXml(java.io.InputStream JSONStream, java.io.OutputStream XMLStream, boolean verbose)
          Method to do the transform from an JSON input stream to a XML stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XML

public XML()
Method Detail

toJson

public static void toJson(java.io.InputStream XMLStream,
                          java.io.OutputStream JSONStream)
                   throws org.xml.sax.SAXException,
                          java.io.IOException
Method to do the transform from an XML input stream to a JSON stream. Neither input nor output streams are closed. Closure is left up to the caller. Same as calling toJson(inStream, outStream, false); (Default is compact form)

Parameters:
XMLStream - The XML stream to convert to JSON
JSONStream - The stream to write out JSON to. The contents written to this stream are always in UTF-8 format.
Throws:
org.xml.sax.SAXException - Thrown is a parse error occurs.
java.io.IOException - Thrown if an IO error occurs.

toJson

public static void toJson(java.io.InputStream XMLStream,
                          java.io.OutputStream JSONStream,
                          boolean verbose)
                   throws org.xml.sax.SAXException,
                          java.io.IOException
Method to do the transform from an XML input stream to a JSON stream. Neither input nor output streams are closed. Closure is left up to the caller.

Parameters:
XMLStream - The XML stream to convert to JSON
JSONStream - The stream to write out JSON to. The contents written to this stream are always in UTF-8 format.
verbose - Flag to denote whether or not to render the JSON text in verbose (indented easy to read), or compact (not so easy to read, but smaller), format.
Throws:
org.xml.sax.SAXException - Thrown if a parse error occurs.
java.io.IOException - Thrown if an IO error occurs.

toJson

public static java.lang.String toJson(java.io.InputStream xmlStream)
                               throws org.xml.sax.SAXException,
                                      java.io.IOException
Method to take an input stream to an XML document and return a String of the JSON format. Note that the xmlStream is not closed when read is complete. This is left up to the caller, who may wish to do more with it. This is the same as toJson(xmlStream,false)

Parameters:
xmlStream - The InputStream to an XML document to transform to JSON.
Returns:
A string of the JSON representation of the XML file
Throws:
org.xml.sax.SAXException - Thrown if an error occurs during parse.
java.io.IOException - Thrown if an IOError occurs.

toJson

public static java.lang.String toJson(java.io.InputStream xmlStream,
                                      boolean verbose)
                               throws org.xml.sax.SAXException,
                                      java.io.IOException
Method to take an input stream to an XML document and return a String of the JSON format. Note that the xmlStream is not closed when read is complete. This is left up to the caller, who may wish to do more with it.

Parameters:
xmlStream - The InputStream to an XML document to transform to JSON.
verbose - Boolean flag denoting whther or not to write the JSON in verbose (formatted), or compact form (no whitespace)
Returns:
A string of the JSON representation of the XML file
Throws:
org.xml.sax.SAXException - Thrown if an error occurs during parse.
java.io.IOException - Thrown if an IOError occurs.

toJson

public static java.lang.String toJson(java.io.File xmlFile,
                                      boolean verbose)
                               throws org.xml.sax.SAXException,
                                      java.io.IOException
Method to take an XML file and return a String of the JSON format.

Parameters:
xmlFile - The XML file to transform to JSON.
verbose - Boolean flag denoting whther or not to write the JSON in verbose (formatted), or compact form (no whitespace)
Returns:
A string of the JSON representation of the XML file
Throws:
org.xml.sax.SAXException - Thrown if an error occurs during parse.
java.io.IOException - Thrown if an IOError occurs.

toJson

public static java.lang.String toJson(java.io.File xmlFile)
                               throws org.xml.sax.SAXException,
                                      java.io.IOException
Method to take an XML file and return a String of the JSON format. This is the same as toJson(xmlStream,false)

Parameters:
xmlFile - The XML file to convert to JSON.
Returns:
A string of the JSON representation of the XML file
Throws:
org.xml.sax.SAXException - Thrown if an error occurs during parse.
java.io.IOException - Thrown if an IOError occurs.

toXml

public static void toXml(java.io.InputStream JSONStream,
                         java.io.OutputStream XMLStream)
                  throws java.io.IOException
Method to do the transform from an JSON input stream to a XML stream. Neither input nor output streams are closed. Closure is left up to the caller. Same as calling toXml(inStream, outStream, false); (Default is compact form)

Parameters:
JSONStream - The JSON stream to convert to XML
XMLStream - The stream to write out XML to. The contents written to this stream are always in UTF-8 format.
Throws:
java.io.IOException - Thrown if an IO error occurs.

toXml

public static void toXml(java.io.InputStream JSONStream,
                         java.io.OutputStream XMLStream,
                         boolean verbose)
                  throws java.io.IOException
Method to do the transform from an JSON input stream to a XML stream. Neither input nor output streams are closed. Closure is left up to the caller.

Parameters:
JSONStream - The XML stream to convert to JSON
XMLStream - The stream to write out JSON to. The contents written to this stream are always in UTF-8 format.
verbose - Flag to denote whether or not to render the XML text in verbose (indented easy to read), or compact (not so easy to read, but smaller), format.
Throws:
java.io.IOException - Thrown if an IO error occurs.

toXml

public static java.lang.String toXml(java.io.InputStream JSONStream)
                              throws java.io.IOException
Method to take an input stream to an JSON document and return a String of the XML format. Note that the JSONStream is not closed when read is complete. This is left up to the caller, who may wish to do more with it. This is the same as toXml(JSONStream,false)

Parameters:
JSONStream - The InputStream to an JSON document to transform to XML.
Returns:
A string of the JSON representation of the XML file
Throws:
java.io.IOException - Thrown if an IOError occurs.

toXml

public static java.lang.String toXml(java.io.InputStream JSONStream,
                                     boolean verbose)
                              throws java.io.IOException
Method to take an input stream to an JSON document and return a String of the XML format. Note that the JSONStream is not closed when read is complete. This is left up to the caller, who may wish to do more with it.

Parameters:
xmlStream - The InputStream to an JSON document to transform to XML.
verbose - Boolean flag denoting whther or not to write the XML in verbose (formatted), or compact form (no whitespace)
Returns:
A string of the JSON representation of the XML file
Throws:
java.io.IOException - Thrown if an IOError occurs.

toXml

public static java.lang.String toXml(java.io.File jsonFile,
                                     boolean verbose)
                              throws java.io.IOException
Method to take a JSON file and return a String of the XML format.

Parameters:
xmlFile - The JSON file to transform to XML.
verbose - Boolean flag denoting whther or not to write the XML in verbose (formatted), or compact form (no whitespace)
Returns:
A string of the XML representation of the JSON file
Throws:
java.io.IOException - Thrown if an IOError occurs.

toXml

public static java.lang.String toXml(java.io.File jsonFile)
                              throws java.io.IOException
Method to take an JSON file and return a String of the XML format. This is the same as toXml(jsonStream,false)

Parameters:
jsonFile - The XML file to convert to XML.
Returns:
A string of the XML representation of the JSON file
Throws:
java.io.IOException - Thrown if an IOError occurs.


Copyright © 2009-2013 The Apache Software Foundation. All Rights Reserved.