Recently, this problem has been distorting me a lot, and I can’t find a possible solution.
I am dealing with a web server that receives an XML document for processing. Server parser has problems with &, ',', <,>. I know this is bad, I have not implemented an XML parser on this server. But before I wait for the update, I need to get around.
Now, before uploading my XML document to this server, I need to parse it and avoid special xml characters. I am currently using the DOM. The problem is that if I repeat TEXT_NODES and replace all special characters with my escaped versions, when I save this document,
for d'exi get d&apos;exbut i needd'ex
This makes sense as the DOM eludes "&". But obviously this is not what I need.
So, if the DOM is already capable of escaping from "&"to "&", how can I convince other characters like "to "?
If it cannot, how can I save the already processed and shielded texts in the nodes in it, without forcing them to re-hide them when saving?
Here's how I avoid the special characters that I used the apache StringEscapeUtils class:
public String xMLTransform() throws Exception
{
String xmlfile = FileUtils.readFileToString(new File(filepath));
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xmlfile.trim().replaceFirst("^([\\W]+)<", "<"))));
NodeList nodeList = doc.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
Node child = currentNode.getFirstChild();
while(child != null) {
if (child.getNodeType() == Node.TEXT_NODE) {
child.setNodeValue(StringEscapeUtils.escapeXml10(child.getNodeValue()));
}
child = child.getNextSibling();
}
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
FileOutputStream fop = null;
File file;
file = File.createTempFile("escapedXML"+UUID.randomUUID(), ".xml");
fop = new FileOutputStream(file);
String xmlString = writer.toString();
byte[] contentInBytes = xmlString.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
return file.getPath();
}