Gyan
Gyan

Reputation: 1176

Special Character and html tag parsing in xml

I have an xml in which it contails <br/> tag in the element data fiels. I am able to parse it using string builder in Sax parser. Now i want to trim down some of the tags and return back an xml response. In the response xml source i am getting the tag replaces as &lt;br/>. My concern is that the "<" gets replaced by &lt; but the ">" tag does not get replaced by &gt;. Anybody has idea of how to sort out this problem.

Upvotes: 0

Views: 969

Answers (1)

Sebastian Juarez
Sebastian Juarez

Reputation: 3361

You should escape for XML. Try EscapeUtils from Apache Commons Lang.

Mind that also Java may be having trouble dealing with it.

I prefer first to escapeJava and after that escapeXML.

Usage:

    String escapedStr= StringEscapeUtils.escapeJava(yourString);
    escapedStr= StringEscapeUtils.escapeXML(yourString);

Apache Commons Lang download link.

Upvotes: 2

Related Questions