Java:在不解码数字字符参考的情况下将字符串转换为文档(W3C)

时间:2019-05-22 09:12:24

标签: java xml ncr

当我将XML格式的字符串格式转换为文档格式时,所有具有数字字符引用(NCR)值的节点值都将解码为人类可读的值。

我想在输出中接收完全相同的XML(不解码NCR)。所需的输出是:

"<urn:Root xmlns:urn=\"http://my.test.com/ABC\"><urn:Status type=\"\">&#1042; &#1088;&#1072;&#1073;&#1086;&#1090;&#1077;</urn:Status></urn:Root>"

但是我得到了

"<urn:Root xmlns:urn=\"http://my.test.com/ABC\"><urn:Status type=\"\">В работе</urn:Status></urn:Root>"

示例: Output

public static void main(String[] args) {
    String xml = "<urn:Root xmlns:urn=\"http://my.test.com/ABC\"><urn:Status type=\"\">&#1042; &#1088;&#1072;&#1073;&#1086;&#1090;&#1077;</urn:Status></urn:Root>";
    System.out.println(xml);
    System.out.println(convertStringToXMLDocument(xml));
} 


public static String convertStringToXMLDocument(String xml) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setExpandEntityReferences(false);
    DocumentBuilder builder = null;


    try(InputStream s = new ByteArrayInputStream(xml.getBytes())) {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(s);
        return convertXMLDocumentToString(doc);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}


public static String convertXMLDocumentToString(Document xmlDocument) {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

0 个答案:

没有答案