简体   繁体   English

在xml中解析特殊字符和html标记

[英]Special Character and html tag parsing in xml

I have an xml in which it contails <br/> tag in the element data fiels. 我有一个xml,它在元素数据fiels中包含<br/>标签。 I am able to parse it using string builder in Sax parser. 我能够使用Sax解析器中的字符串构建器来解析它。 Now i want to trim down some of the tags and return back an xml response. 现在我想减少一些标签并返回一个xml响应。 In the response xml source i am getting the tag replaces as &lt;br/> . 在响应xml源代码中,我将标记替换为&lt;br/> My concern is that the "<" gets replaced by &lt; 我担心的是“<”会被&lt; but the ">" tag does not get replaced by &gt; 但“>”标签不会被&gt;取代 . Anybody has idea of how to sort out this problem. 任何人都知道如何解决这个问题。

You should escape for XML. 你应该逃避XML。 Try EscapeUtils from Apache Commons Lang . 尝试使用Apache Commons Lang的EscapeUtils

Mind that also Java may be having trouble dealing with it. 请注意,Java也可能无法处理它。

I prefer first to escapeJava and after that escapeXML. 我首先选择escapeJava,然后再使用escapeXML。

Usage: 用法:

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

Apache Commons Lang download link. Apache Commons Lang下载链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM