title: Serialize a DOM document in UTF-8
body: <p>I have been trying to get rid of all  com.sun.org.apache.xml.internal packages in my code replacing them with stable alternatives.</p>
  
  <p>This is one method I've replaced...</p>
  
  <pre><code>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
  </code></pre>
  
  <p>this is an alternative I found...</p>
  
  <pre><code>/**
   * @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
  </code></pre>
  
  <p>However, its showing a difference in the xml that is being produced.</p>
  
  <p>It creates </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
  </code></pre>
  
  <p>How can this be modified to create UTF-8 instead?</p>