【问题标题】:Parsing XML with keys that have spaces in their names使用名称中包含空格的键解析 XML
【发布时间】:2010-10-07 02:21:25
【问题描述】:

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

例如,

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

在上面的例子中,'this is' 导致解析器出错。我猜它是因为它认为它是一个预期的属性??

对于奖金 PT, (如果键是一个数字......像'1',同样的事情也会发生)......

【问题讨论】:

  • 实际上解析器认为is是一个属性,如解析器错误信息Entity: line 1: parser error : Specification mandate value for attribute is所示

标签: php parsing simplexml


【解决方案1】:

这是因为将数字作为元素和带有空格的元素不是有效 XML 的一部分。您最好在 XML 字符串上运行替换函数,将 &lt;(\d+)&gt; 转换为 &lt;el_$1&gt;,并将节点中的空格也替换为下划线。

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2015-09-06
    • 2015-09-30
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多