如何处理 XML 中 URL 中的特殊字符 [英] How to deal with special characters in URLs inside XML

查看:48
本文介绍了如何处理 XML 中 URL 中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 元素,它有一个 url 作为它的子元素之一,例如:
http://maps.google.com/FortWorth&Texas,more+url;data

I have an XML element that has a url as one of it's children, for example:
http://maps.google.com/FortWorth&Texas,more+url;data

在解析这个时,我有两个问题:
1.) (&) 符号会破坏整个解析,除非被替换为 &amp(会破坏 url)
2.) 逗号 (,) 尝试将我的解析器发送到下一个子节点,从而导致 url 不完整.

When parsing this, I'm having two issues:
1.) The (&) symbol breaks the entire parse unless replaced with &amp (which breaks the url)
2.) The comma (,) tries to send my parser on to the next child, resulting in an incomplete url.

我该怎么做才能解决这个问题?
我正在使用 Javascript 和 PHP.

What can I do to remedy this?
I'm using Javascript and PHP.

推荐答案

& 替换 & 不应该破坏 url.您是否遗漏了 ;?

Replacing & with & shouldn't break the url. Did you left out the ;?

更好的解决方案是您应该将其包装在 CDATA 标签中:

Better solution is you should wrap that in a CDATA tag:

<代码><![CDATA[ http://maps.google.com/FortWorth&Texas,more+url;数据 ]]>

<![CDATA[ http://maps.google.com/FortWorth&Texas,more+url;data ]]>

它告诉 XML 解析器将其视为文本而不是解析 &.

Which tells the XML parser to treat it as text and not parse the &.

这篇关于如何处理 XML 中 URL 中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆