如何在 java 中对字符串进行编码以使 xHTML 有效

发布于 2024-12-04 15:00:26 字数 453 浏览 1 评论 0原文

我正在用 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 &lt;3&gt; times a year &amp 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-12-11 15:00:26

这个问题之前已经被问过 这里,但它需要依赖外部库,因为 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.

捂风挽笑 2024-12-11 15:00:26

这与您的问题不完全匹配,但看来答案就是您正在寻找的在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文