使用名称中包含空格的键解析 XML

发布于 2024-09-26 05:34:54 字数 337 浏览 1 评论 0原文

有谁知道当键中有空格时如何使用 SimpleXMLElement 解析 php 中的 xml 字符串?

例如,

$xmlString = "<test><this is>a</this is></test>";
$xml = new SimpleXMLElement($xmlString);
print_r($xml);

在上面的示例中,“this is”会导致解析器崩溃。我猜它是因为它认为它是一个属性,正如预期的那样?

为了获得奖励 PT, (如果键是数字......比如“1”,也会发生同样的事情)..

Does anyone know how to parse an xml string in php using SimpleXMLElement when the key has a space in it ?

For example,

$xmlString = "<test><this is>a</this is></test>";
$xml = new SimpleXMLElement($xmlString);
print_r($xml);

in the above example, 'this is' causes the parser to go bananas. I'm guessing its because it thinks its a property as is expecting like ??

FOR A BONUS PT,
(also if the key is a number.. like '1', the same thing happens)..

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

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

发布评论

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

评论(1

⊕婉儿 2024-10-03 05:34:54

这是因为将数字作为元素以及将空格作为元素并不是有效 XML 的一部分。您最好在 XML 字符串上运行替换函数,将 <(\d+)> 转换为 ,然后替换中的空格也带有下划线的节点。

That's because having numbers as elements and elements with spaces is not part of valid XML. You're probably better off running a replacement function on your XML string, converting <(\d+)> to <el_$1>, and replacing spaces in nodes with underscores as well.

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