Escape Characters in Text Node
843834Oct 6 2003 — edited Mar 10 2006I am creating a document that contains text nodes potentially with characters I believe need to be escaped (< > ' " &) and am finding some pecular behavior. I am using Xerces.
Here's my test code:
// Create a document...
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
// Add a test node....
Node valueNode = doc.appendChild(doc.createElement("value"));
valueNode.appendChild(doc.createTextNode("< > ' \" &"));
// Print it out...
StreamResult result = new StreamResult(System.out);;
DOMSource source = new DOMSource(doc);
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
When I run this I get:
<?xml version="1.0" encoding="UTF-8"?>
<value>< > ' " &</value>
I am trying to figure why the apostrophe and quote characters not escaped but the other 3 are.
If I escape them myself (using ' and " in the createTextNode argument), I get this output which I think is not correct:
<?xml version="1.0" encoding="UTF-8"?>
<value>< > ' " &</value>
How can I get all 5 or none of the characters to be escaped? Is it OK by the spec to have apostrophes and quotes in text nodes?
Thanks in advance for any help,
Marc