public static void transform ( InputStream XMLStream , OutputStream JSONStream , boolean verbose ) throws SAXException , IOException { if ( logger . isLoggable ( Level . FINER ) ) { logger . entering ( className , "transform(InputStream, OutputStream)" ) ; } if ( XMLStream == null ) { throw new NullPointerException ( "XMLStream cannot be null" ) ; } else if ( JSONStream == null ) { throw new NullPointerException ( "JSONStream cannot be null" ) ; } else { if ( logger . isLoggable ( Level . FINEST ) ) { logger . logp ( Level . FINEST , className , "transform" , "Fetching a SAX parser for use with JSONSAXHandler" ) ; } try { /** * Get a parser. */ SAXParserFactory factory = SAXParserFactory . newInstance ( ) ; factory . setNamespaceAware ( true ) ; SAXParser sParser = factory . newSAXParser ( ) ; XMLReader parser = sParser . getXMLReader ( ) ; JSONSAXHandler jsonHandler = new JSONSAXHandler ( JSONStream , verbose ) ; parser . setContentHandler ( jsonHandler ) ; parser . setErrorHandler ( jsonHandler ) ; InputSource source = new InputSource ( new BufferedInputStream ( XMLStream ) ) ; if ( logger . isLoggable ( Level . FINEST ) ) { logger . logp ( Level . FINEST , className , "transform" , "Parsing the XML content to JSON" ) ; } /** * Parse it. */ source . setEncoding ( "UTF-8" ) ; parser . parse ( source ) ; jsonHandler . flushBuffer ( ) ; } catch ( javax . xml . parsers . ParserConfigurationException pce ) { throw new SAXException ( "Could not get a parser: " + pce . toString ( ) ) ; } } if ( logger . isLoggable ( Level . FINER ) ) { logger . exiting ( className , "transform(InputStream, OutputStream)" ) ; } }
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 .