当我将XML格式的字符串格式转换为文档格式时,所有具有数字字符引用(NCR)值的节点值都将解码为人类可读的值。
我想在输出中接收完全相同的XML(不解码NCR)。所需的输出是:
"<urn:Root xmlns:urn=\"http://my.test.com/ABC\"><urn:Status type=\"\">В работе</urn:Status></urn:Root>"
但是我得到了
"<urn:Root xmlns:urn=\"http://my.test.com/ABC\"><urn:Status type=\"\">В работе</urn:Status></urn:Root>"
示例:
public static void main(String[] args) {
String xml = "<urn:Root xmlns:urn=\"http://my.test.com/ABC\"><urn:Status type=\"\">В работе</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;
}