1

コード内のすべてのcom.sun.org.apache.xml.internalパッケージを削除して、安定した代替パッケージに置き換えようとしています。

これは私が置き換えた方法の1つです...

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

これは私が見つけた代替案です...

/**
 * @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

ただし、生成されているxmlの違いを示しています。

作成します

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

代わりにUTF-8を作成するためにこれをどのように変更できますか?

4

1 に答える 1

0

使用する必要があると思いますLSOutput

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

詳細については、こちらの回答を参照してください。

com.sun.org.apache.xml.internal.serialize.XMLSerializer と com.sun.org.apache.xml.internal.serialize.OutputFormat を変更します。

于 2012-07-02T21:41:17.130 に答える