public String writeToString ( Node nodeArg ) throws DOMException , LSException { // return null is nodeArg is null. Should an Exception be thrown instead? if ( nodeArg == null ) { return null ; } // Should we reset the serializer configuration before each write operation? // Obtain a reference to the serializer to use Serializer serializer = fXMLSerializer ; serializer . reset ( ) ; if ( nodeArg != fVisitedNode ) { // Determine the XML Document version of the Node String xmlVersion = getXMLVersion ( nodeArg ) ; serializer . getOutputFormat ( ) . setProperty ( "version" , xmlVersion ) ; // Set the output encoding and xml version properties fDOMConfigProperties . setProperty ( DOMConstants . S_XERCES_PROPERTIES_NS + DOMConstants . S_XML_VERSION , xmlVersion ) ; fDOMConfigProperties . setProperty ( DOMConstants . S_XSL_OUTPUT_ENCODING , "UTF-16" ) ; // If the node to be serialized is not a Document, Element, or Entity // node // then the XML declaration, or text declaration, should be never be // serialized. if ( ( nodeArg . getNodeType ( ) != Node . DOCUMENT_NODE || nodeArg . getNodeType ( ) != Node . ELEMENT_NODE || nodeArg . getNodeType ( ) != Node . ENTITY_NODE ) && ( ( fFeatures & XMLDECL ) != 0 ) ) { fDOMConfigProperties . setProperty ( DOMConstants . S_XSL_OUTPUT_OMIT_XML_DECL , DOMConstants . DOM3_DEFAULT_FALSE ) ; } fVisitedNode = nodeArg ; } // Update the serializer properties fXMLSerializer . setOutputFormat ( fDOMConfigProperties ) ; // StringWriter to Output to StringWriter output = new StringWriter ( ) ; // try { // Set the Serializer's Writer to a StringWriter serializer . setWriter ( output ) ; // Get a reference to the serializer then lets you serilize a DOM // Use this hack till Xalan support JAXP1.3 if ( fDOMSerializer == null ) { fDOMSerializer = ( DOM3Serializer ) serializer . asDOM3Serializer ( ) ; } // Set the error handler on the DOM3Serializer interface implementation if ( fDOMErrorHandler != null ) { fDOMSerializer . setErrorHandler ( fDOMErrorHandler ) ; } // Set the filter on the DOM3Serializer interface implementation if ( fSerializerFilter != null ) { fDOMSerializer . setNodeFilter ( fSerializerFilter ) ; } // Set the NewLine character to be used fDOMSerializer . setNewLine ( fEndOfLine . toCharArray ( ) ) ; // Serializer your DOM, where node is an org.w3c.dom.Node fDOMSerializer . serializeDOM3 ( nodeArg ) ; } catch ( LSException lse ) { // Rethrow LSException. throw lse ; } catch ( RuntimeException e ) { throw ( LSException ) createLSException ( LSException . SERIALIZE_ERR , e ) . fillInStackTrace ( ) ; } catch ( Exception e ) { if ( fDOMErrorHandler != null ) { fDOMErrorHandler . handleError ( new DOMErrorImpl ( DOMError . SEVERITY_FATAL_ERROR , e . getMessage ( ) , null , e ) ) ; } throw ( LSException ) createLSException ( LSException . SERIALIZE_ERR , e ) . fillInStackTrace ( ) ; } // return the serialized string return output . toString ( ) ; }
Serializes the specified node and returns a String with the serialized data to the caller .