Java xml Transformer 转义&
我遇到了 javax.xml.transform.Transformer
的问题。
我正在尝试创建一个 XML 文档,其中一个属性是 HTTP 链接,其中包含用于查询的 &
。调用 transform()
方法后,所有 &
字符都变为 &
。
那么有什么方法可以配置 Transformer 不将 &
转换为 &
吗?提前致谢。
编辑
在我尝试创建的 xml 文档中,属性之一是带有查询字符串的 http 链接。所以我想要的结果是这样的
<Notification url="http://www.xyz.com/notify.jsp?param1=123¶m2=345/>
但是我现在得到的是
<Notification url="http://www.xyz.com/notify.jsp?param1=123&param2=345/>
因为我必须将 xml 文档转换为字符串并通过套接字发送它。所以我不希望将 &
转换为 &
作为最终结果。
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¶m2=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这正是必须发生的情况。
是什么让您认为
是正确的 XML/HTML,而诸如
之类的内容则不是?
&
必须在任何地方进行转义,包括属性值,例如href
。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 ashref
.