java - Serialize a DOM document in UTF-8 -
i have been trying rid of com.sun.org.apache.xml.internal packages in code replacing them stable alternatives.
this 1 method i've replaced...
import com.sun.org.apache.xml.internal.serialize.lineseparator; import com.sun.org.apache.xml.internal.serialize.outputformat; import com.sun.org.apache.xml.internal.serialize.xmlserializer; ... ... /** * @param source * @param target * @throws ioexception */ protected static void serialize( document source, writer target ) throws ioexception { outputformat outputformat = new outputformat( (document) source ); outputformat.setlineseparator( lineseparator.windows ); // format.setindenting(true); outputformat.setlinewidth( 0 ); outputformat.setpreservespace( true ); xmlserializer serializer = new xmlserializer( target, outputformat ); serializer.asdomserializer(); serializer.serialize( source ); } // end serialize
this alternative found...
/** * @param source * @param target * @throws ioexception * @throws illegalaccessexception * @throws instantiationexception * @throws classnotfoundexception * @throws classcastexception */ protected static void serialize( document source, writer target ) throws exception { domimplementationregistry registry = domimplementationregistry.newinstance(); domimplementationls impl = (domimplementationls) registry.getdomimplementation( "ls" ); lsserializer writer = impl.createlsserializer(); target.write( writer.writetostring(source) ); } // end serialize
however, showing difference in xml being produced.
it creates
<?xml version="1.0" encoding="utf-16"?>
how can modified create utf-8 instead?
i think have use lsoutput
:
lsoutput domoutput = impl.createlsoutput(); domoutput.setencoding(standardcharsets.utf_8.name()); domoutput.setcharacterstream(stringwriter);
see response here more details:
Comments
Post a Comment