如何在 Jsoup 中编写 & 字符

发布于 2024-12-17 07:46:53 字数 372 浏览 3 评论 0原文

我正在使用 Jsoup 来解析和修改一些 HTML。在某些地方,我想向 HTML 添加不间断空格实体 ( )。我以为我可以像这个简化的例子一样做到这一点:

Element paragraph = someDocument.select("p").first();
paragraph.text("First sentence.  Second sentence.");

但是 Jsoup 将我的   转换为   有效地编码&符号本身。我想我真正的问题是:如何手动将 & 符号写入 Element 的文本?

I'm using Jsoup to parse and modify some HTML. In certain places, I want to add a non-breaking space entity ( ) to the HTML. I assumed I could do it as in this simplified example:

Element paragraph = someDocument.select("p").first();
paragraph.text("First sentence.  Second sentence.");

But Jsoup turns my   into   effectively encoding the ampersand itself. I guess my real question is: how can I manually write an ampersand character to the text of an Element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烟雨扶苏 2024-12-24 07:46:53

你正在做Element.text。如果是 html,请使用 .html(String s) 代替,将代码替换为

Element paragraph = someDocument.select("p").first();
paragraph.html("First sentence.  Second sentence.");

You are doing Element.text. If its html, use .html(String s) instead so, replace your code with

Element paragraph = someDocument.select("p").first();
paragraph.html("First sentence.  Second sentence.");
荆棘i 2024-12-24 07:46:53

尝试使用 unicode 值作为不间断空格。

Element paragraph = someDocument.select("p").first();
paragraph.text("First sentence.\u00a0Second sentence.");

Try using the unicode value for no-breaking space.

Element paragraph = someDocument.select("p").first();
paragraph.text("First sentence.\u00a0Second sentence.");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文