Java xml Transformer 转义&

发布于 2024-10-09 09:53:37 字数 720 浏览 0 评论 0原文

我遇到了 javax.xml.transform.Transformer 的问题。

我正在尝试创建一个 XML 文档,其中一个属性是 HTTP 链接,其中包含用于查询的 & 。调用 transform() 方法后,所有 & 字符都变为 &

那么有什么方法可以配置 Transformer 不将 & 转换为 & 吗?提前致谢。

编辑


在我尝试创建的 xml 文档中,属性之一是带有查询字符串的 http 链接。所以我想要的结果是这样的

<Notification url="http://www.xyz.com/notify.jsp?param1=123&param2=345/>

但是我现在得到的是

<Notification url="http://www.xyz.com/notify.jsp?param1=123&amp;param2=345/>

因为我必须将 xml 文档转换为字符串并通过套接字发送它。所以我不希望将 & 转换为 &amp; 作为最终结果。

I am having a problem with javax.xml.transform.Transformer.

I am trying to create a XML document and one of the attributes is HTTP link which contains & for query. After I've invoked the transform() method, all the & characters become &.

So is there any way that I can configure Transformer not to convert & to &? Thanks in advance.

Edit


In the xml document that I am trying to create, one of the attributes is a http link with query string. So what I would like to have as a result is something like

<Notification url="http://www.xyz.com/notify.jsp?param1=123¶m2=345/>

But I am getting now is

<Notification url="http://www.xyz.com/notify.jsp?param1=123&param2=345/>

Because I have to transform the xml document into a String and send it over socket. So I do not want & converted to & as final result.

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

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

发布评论

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

评论(1

我乃一代侩神 2024-10-16 09:53:37

调用转换方法后,所有&字符都变成&

这正是必须发生的情况。

是什么让您认为

A & B

是正确的 XML/HTML,而诸如

之类的内容则不是?

& 必须在任何地方进行转义,包括属性值,例如 href

After I've invoked transform method, all the & characters become &

That's exactly what has to happen.

What makes you assume that <div>A & B</div> is correct XML/HTML and something like <div title="A & B" /> is not?

The & has to be escaped absolutely everywhere, that includes attribute values, such as href.

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