Package com.ibm.json.xml
Class XMLToJSONTransformer
java.lang.Object
com.ibm.json.xml.XMLToJSONTransformer
This class is a static helper for various ways of converting an XML document/InputStream into a JSON stream or String.
For example, the XML document:
First item
Second item
Third item
Text!
Text!
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"
}
}
}
in JSON (in non-compact form) becomes
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Method to take an XML file and return a String of the JSON format.static String
Method to take an XML file and return a String of the JSON format.static String
transform
(InputStream xmlStream) Method to take an input stream to an XML document and return a String of the JSON format.static String
transform
(InputStream xmlStream, boolean verbose) Method to take an input stream to an XML document and return a String of the JSON format.static void
transform
(InputStream XMLStream, OutputStream JSONStream) Method to do the transform from an XML input stream to a JSON stream.static void
transform
(InputStream XMLStream, OutputStream JSONStream, boolean verbose) Method to do the transform from an XML input stream to a JSON stream.
-
Constructor Details
-
XMLToJSONTransformer
public XMLToJSONTransformer()
-
-
Method Details
-
transform
public static void transform(InputStream XMLStream, OutputStream JSONStream) throws SAXException, 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 transform(inStream, outStream, false); (Default is compact form)- Parameters:
XMLStream
- The XML stream to convert to JSONJSONStream
- The stream to write out JSON to. The contents written to this stream are always in UTF-8 format.- Throws:
SAXException
- Thrown is a parse error occurs.IOException
- Thrown if an IO error occurs.
-
transform
public static void transform(InputStream XMLStream, OutputStream JSONStream, boolean verbose) throws SAXException, 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 JSONJSONStream
- 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:
SAXException
- Thrown if a parse error occurs.IOException
- Thrown if an IO error occurs.
-
transform
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 transform(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:
SAXException
- Thrown if an error occurs during parse.IOException
- Thrown if an IOError occurs.
-
transform
public static String transform(InputStream xmlStream, boolean verbose) throws SAXException, 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:
SAXException
- Thrown if an error occurs during parse.IOException
- Thrown if an IOError occurs.
-
transform
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:
SAXException
- Thrown if an error occurs during parse.IOException
- Thrown if an IOError occurs.
-
transform
Method to take an XML file and return a String of the JSON format. This is the same as transform(xmlStream,false)- Parameters:
xmlFile
- The XML file to convert to JSON.- Returns:
- A string of the JSON representation of the XML file
- Throws:
SAXException
- Thrown if an error occurs during parse.IOException
- Thrown if an IOError occurs.
-