public void parse ( ) throws ParserException { try { jsonParser . nextToken ( ) ; contentHandler . startDocument ( ) ; if ( shouldAddArtificialRoot ( ) ) { startElement ( artificialRootName ) ; parseElement ( artificialRootName , false ) ; endElement ( artificialRootName ) ; } else if ( START_OBJECT . equals ( jsonParser . getCurrentToken ( ) ) ) { int elementsWritten = parseObject ( ) ; if ( elementsWritten > 1 ) { throw new ParserException ( "More than one root element. Can not generate legal XML. You can set artificialRootName to generate an artificial root." ) ; } } else { throw new ParserException ( "Unsupported root element. Can not generate legal XML. You can set artificialRootName to generate an artificial root." ) ; } contentHandler . endDocument ( ) ; } catch ( Exception e ) { throw new ParserException ( "Parsing error: " + e . getMessage ( ) , e ) ; } }
Method parses JSON and emits SAX events .