【问题标题】:PHP parse <cpe-23:cpe23-item name=" ... ">?PHP 解析 <cpe-23:cpe23-item name=" ... ">?
【发布时间】:2015-01-18 17:13:55
【问题描述】:

我无法使用 SimpleXML PHP 解析这个标签。

这是我的文件:

<cpe-item name="cpe:/a:%240.99_kindle_books_project:%240.99_kindle_books:6::">
    <cpe-23:cpe23-item name="cpe:2.3:a:\*:*:*:*:*:*:*:*:*:*"/>
</cpe-item>

我想解析 cpe-23:cpe23-item 上的名称内容。

这是我现在的代码:

foreach ($xml->{'cpe-item'} as $cpe) {
  echo $cpe->children('cpe-23', TRUE) // This is the line I have to modify
}

【问题讨论】:

    标签: php xml simplexml


    【解决方案1】:

    $cpe-&gt;children('cpe-23', TRUE) 返回命名空间中前缀为cpe-23: 的所有子项。

    要找到特定的孩子,最简单的方法是通过名称引用它,在本例中为-&gt;cpe23-item,因为元素是&lt;cpe23-item ...&gt;。但是,这会混淆 PHP 的解析器,因为- 通常表示“减号”;您可以在名称周围使用{} 来避免这种情况,例如-&gt;{'cpe23-item'},名称本身封装在撇号中。

    要获取该元素的属性,您通常会使用数组访问,例如['name']。然而,XML 命名空间的一个怪癖意味着这里的name= 属性在没有命名空间,所以你必须使用-&gt;attributes() 函数来“离开”你“进入”的 cpe-23 命名空间与-&gt;children() 通话,给你-&gt;attributes(null)-&gt;name

    综合起来,你有:

    echo $cpe->children('cpe-23', TRUE)->{'cpe23-item'}->attributes(null)->name;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2014-07-30
      • 1970-01-01
      相关资源
      最近更新 更多