1

I have been trying to get rid of all com.sun.org.apache.xml.internal packages in my code replacing them with stable alternatives.

This is one 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 is an alternative I 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, its showing a difference in the xml that is being produced.

It creates

<?xml version="1.0" encoding="UTF-16"?>

How can this be modified to create UTF-8 instead?

bluish
  • 26,356
  • 27
  • 122
  • 180
speedos82
  • 97
  • 1
  • 2
  • 7
  • There are many code snippets in the net with reading and writing XML. `org.w3c.Document.getXmlEncoding()` would give "UTF-16" after reading. But there is no setter as the writing uses an encoding which should be patched in the `encoding` attribute. – Joop Eggen Jul 02 '12 at 21:23

1 Answers1

0

I think you have to use LSOutput:

LSOutput domOutput = impl.createLSOutput();
domOutput.setEncoding(StandardCharsets.UTF_8.name());
domOutput.setCharacterStream(stringWriter);

See the response here for more details:

Change the com.sun.org.apache.xml.internal.serialize.XMLSerializer & com.sun.org.apache.xml.internal.serialize.OutputFormat

Community
  • 1
  • 1
Dave L.
  • 9,595
  • 7
  • 43
  • 69