PHP DOMDocument->loadXML,其中 XML 包含与号/更少/更大?

发布于 2024-08-17 23:03:33 字数 348 浏览 5 评论 0原文

我正在尝试解析包含字符 & 的 XML 字符串。 <和>在文本数据中。通常,这些字符应该是 htmlencoded,但在我的情况下,它们不是,所以我收到以下消息:

警告:DOMDocument :: loadXML()[function.loadXML]:错误解析实体中的属性名称... 警告: DOMDocument::loadXML() [function.loadXML]: 无法找到开始标记的结尾...

我可以使用 str_replace 对所有 & 进行编码,但如果我使用 < 进行编码或>我也在为有效的 XML 标签做这件事。

有谁知道这个问题的解决方法?

谢谢你!

I'm trying to parse an XML string containing characters & < and > in the TEXTDATA. Normally, those characters should be htmlencoded, but in my case they aren't so I get the following messages:

Warning: DOMDocument::loadXML() [function.loadXML]: error parsing attribute name in Entity ...
Warning: DOMDocument::loadXML() [function.loadXML]: Couldn't find end of Start Tag ...

I can use the str_replace to encode all the &, but if I do that with < or > I'm doing it for valid XML tags too.

Does anyone know a workaround for this problem??

Thank you!

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

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

发布评论

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

评论(4

病毒体 2024-08-24 23:03:33

如果您有 < XML 中的文本内部...这不是有效的 XML。尝试对其进行编码或将它们包含在 ] 中。

如果不可能(因为你没有输出这个“XML”),我建议尝试使用一些 Html 解析库(我没有使用它们,但它们存在),因为它们没有 XML 严格。

但在尝试任何其他事情之前我真的会尝试获取有效的 XML!

If you have a < inside text in an XML... it's not a valid XML. Try to encode it or to enclose them into <![CDATA[.

If it's not possible (because you're not outputting this "XML") I'd suggest to try with some Html parsing library (I didn't used them, but they exists) beacuse they're less strict than XML ones.

But I'd really try to get valid XML before trying any other thing!!

吐个泡泡 2024-08-24 23:03:33

我经常在 DomDocument 的 load() 调用前面使用 @ 主要是因为你永远无法绝对确定你加载的内容是你所期望的。

使用 @ 将抑制错误。

@$dom->loadXml($myXml);

I often use @ in front of calls to load() for DomDocument mainly because you can never be absolutely sure what you load, is what you expected.

Using @ will suppress errors.

@$dom->loadXml($myXml);
孤独患者 2024-08-24 23:03:33

我可以使用 str_replace 对所有 & 进行编码,但如果我使用

来编码或>我也在为有效的 XML 标签执行此操作。

作为严格的临时修复措施,您可以替换那些不属于标签或实体引用的内容,例如:

$str= preg_replace('<(?![a-zA-Z_!?])', '<', $str);
$str= preg_replace('&(?!([a-zA-Z]+|#[0-9]+|#x[0-9a-fA-F]+);)', '&', $str);

但是,这并不是无懈可击的,从长远来看,您需要修复生成此虚假标记的任何内容,或者对需要修复的人大喊大叫,直到他们得到线索。根据定义,像这样的格式不正确的 XML根本就不是 XML

I can use the str_replace to encode all the &, but if I do that with < or > I'm doing it for valid XML tags too.

As a strictly temporary fixup measure you can replace the ones that aren't part of what looks like a tag or entity reference, eg.:

$str= preg_replace('<(?![a-zA-Z_!?])', '<', $str);
$str= preg_replace('&(?!([a-zA-Z]+|#[0-9]+|#x[0-9a-fA-F]+);)', '&', $str);

However this isn't watertight and in the longer term you need to fix whatever is generating this bogus markup, or shout at the person who needs to fix it until they get a clue. Grossly-non-well-formed XML like this is simply not XML by definition.

明月夜 2024-08-24 23:03:33

将所有文本放入 CDATA 元素中?

<!-- Old -->
<blah>
    x & y < 3
</blah>

<!-- New -->
<blah><![CDATA[
    x & y < 3
]]></blah>

Put all your text inside CDATA elements?

<!-- Old -->
<blah>
    x & y < 3
</blah>

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