Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Escape Characters in Text Node

843834Oct 6 2003 — edited Mar 10 2006
I 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 &apos; and " in the createTextNode argument), I get this output which I think is not correct:

<?xml version="1.0" encoding="UTF-8"?>
<value>< > &apos; &quot; &</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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2006
Added on Oct 6 2003
9 comments
1,218 views