有没有办法防止Java在使用DOM写入XML时转义特殊字符?我不能改变XML的格式,所以我不能将它们保存在CDATA标记中。
我正在尝试使用DOM将HTML保存在XML文件中。目前它正在对特殊字符进行转义,因此将<p>
保存为<p>
。
public void updateXML(Document doc) throws XPathExpressionException{
Node aNode = getXMLNode("//PRECISSCHE.HTM/html/body", doc);
removeChilds(aNode);
aNode.setTextContent(saved_precis_Scheme);
}
它输出为:
<PRECISSCHE.HTM>
<html>
<body><p>schemeName</p></body>
</html>
</PRECISSCHE>
但我需要:
<PRECISSCHE.HTM>
<html>
<body><p>schemeName</p></body>
</html>
</PRECISSCHE>
下面是我创建doc对象的代码:
//pull data and write to XML
public void updateXML(String path) throws XPathExpressionException{
try {
String filepath = path;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
Tab_Client.updateClientXML(doc);
if(!polType.equals("Household")){
Tab_Vehicle.updateVehicleXML(doc);
Tab_PCClaimsConv.updateXML(doc);
Tab_precis.updateXML(doc);
}
if(polType.equals("Household")){
Tab_PropertyDetails.updatePropertyDetailsXML(doc);
Tab_HCsumInsured.updateXML(doc);
Tab_Contents.updateXML(doc);
Tab_HCClaims.updateXML(doc);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
发布于 2019-04-29 09:44:51
您可以在输出数据之前执行stringString.replaceAll("<","<").replaceAll(">",">")
https://stackoverflow.com/questions/55893462
复制相似问题