在TransformerFactory中避免使用XML转义值(例如>)

时间:2014-10-15 09:56:36

标签: java xml dom jaxb marshalling

我正在使用TransformerFactory将xml文档写入文件。 XML文档包含像'>'这样的字符,但是在将其编组而不是“>”时它的印刷“& gt;”   对于马歇尔和unmarshall,我正在使用JAXB-Binder。

private void writeIntoFile( Document document, File xmlPath ){
    document.createProcessingInstruction( StreamResult.PI_DISABLE_OUTPUT_ESCAPING, "");

    TransformerFactory tf = TransformerFactory.newInstance();
    try{
        Transformer t = tf.newTransformer();
        t.setOutputProperty( OutputKeys.METHOD, "xml" );
        t.setOutputProperty( OutputKeys.INDENT, "yes" );
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        t.transform( new DOMSource( document ), new StreamResult( System.out ) );
    }
    catch( TransformerException e ){
        logger.error( e, "Exception while marshalling the document in the xml file [", xmlPath.getAbsolutePath() );
    }
}

0 个答案:

没有答案