Skip to content
Advertisement

How to preserve “Character Reference Codes”(<) while reading content from XML file

I have used below code to read content from xml file

JavaScript

My input file is shown below:

JavaScript

Output is

JavaScript

In this regard , i request to let me know how to retrieve &lt; as is, instead of “<“.since it is not starting of tag but a value of tag “position”

Advertisement

Answer

Using text-commons org.apache.commons.text.StringEscapeUtils class escapeXml10 method, we can escape the character reference codes in the xml tags – StringEscapeUtils.escapeXml10(element.getValue())

Full example is shown below

JavaScript

Same input file used in question:

JavaScript

got expected output is(value of position tag without parsing)

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement