如何在 java 中对字符串进行编码以使 xHTML 有效
我正在用 Java 创建一个 Web 服务,我想清理我的字符串,使它们成为有效的 xHTML。我还想保留换行符。
例如,如果我的用户输入如下字符串:
This course is offered <3> times a year & is scheduled for:
October 2011
November 2012 and
December 2013
我想将字符串清理为如下所示:
This course is offered <3> times a year & is scheduled for:
October 2011
November 2012 and
December 2013
如何在 Java 中实现此目的?
谢谢
I am creating a web service in Java and I want to clean up my strings so they are valid xHTML. I'd also like to preserve line breaks.
So for example, if my user enters a string like this:
This course is offered <3> times a year & is scheduled for:
October 2011
November 2012 and
December 2013
I want to clean up the string to look like this:
This course is offered <3> times a year & is scheduled for:
October 2011
November 2012 and
December 2013
How can I achieve this in Java?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题之前已经被问过 这里,但它需要依赖外部库,因为 JDK 不直接支持它。一探究竟。页面顶部的答案。
This question has been asked before here , but it requires taking a dependency on an external library since the JDK doesn't support it directly. check it out. The top answer on the page.
这与您的问题不完全匹配,但看来答案就是您正在寻找的在 Java 中转义 HTML 的推荐方法。
摘要:来自 apache commons 的 StringEscapeUtils.escapeHtml()
This doesn't match your question exactly, but it appears that the answer is what you're looking for Recommended method for escaping HTML in Java.
Summary: StringEscapeUtils.escapeHtml() from apache commons