Wednesday, 7 January 2015

Escape xml characters within nodes of string xml in java



I have a string of XML data. I need to escape the values within the nodes, but not the nodes themselves.


Ex:

<node1>R&R</node1>

should escape to:

<node1>R&amp;R</node1>

should not escape to:

&lt;node1&gt;R&amp;R&lt;/node1&gt;


I have been working on this for the last couple of days, but haven't had much success. I'm not an expert with Java, but the following are things that I have tried that will not work:



  1. Parsing string xml into a document. Does not work since the data within the nodes contains invalid xml data.

  2. Escaping all of the characters. Does not work since the program receiving this data will not accept it in this format.

  3. Escaping all characters then parsing into document. Throws all sorts of errors.


Any help would be much appreciated.


No comments:

Post a Comment