本文介绍了循环访问 SimpleXMLElement 以访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SimpleXML 检索过程数据,但遇到了很大的困难.我在这里阅读了许多关于这个主题的主题,它们看起来都像我在做什么,但我的却不起作用.这是我所拥有的:

I am trying process data retrieved with SimpleXML and am having great difficulty. I have read numerous threads here about this subject, they all LOOK like what I am doing, but mine are not working. Here's what I've got:

<ROOT>
    <ROWS COMP_ID="165462">
    <ROWS COMP_ID="165463">
</ROOT>

我的代码:

$xml = simplexml_load_file('10.xml');
foreach( $xml->ROWS as $comp_row ) {
    $id = $comp_row->COMP_ID;
}

当我在调试器中逐步执行此操作时,我可以看到 $id 未设置为 COMP_ID 的字符串值,而是成为包含 CLASSNAME 对象的 SimpleXMLElement 本身.我尝试了许多解决此属性的变体,但都没有奏效,包括 $comp_row->attributes()->COMP_ID 等.

As I step through this in my debugger, I can see that $id is not set to the string value of COMP_ID, but becomes a SimpleXMLElement itself containing the CLASSNAME object. I've tried many variations of addressing this attribute but none work, including $comp_row->attributes()->COMP_ID and others.

我错过了什么?

推荐答案

SimpleXML 是一个类似数组的对象.备忘单:

SimpleXML is an array-like object. Cheat sheet:

  • 无前缀的子元素作为数字索引或可遍历
    • 不包括前缀元素(注意,我的意思是前缀,而不是空命名空间SimpleXMLElement命名空间的处理很奇怪,可以说是坏的.)
    • 第一个孩子:$sxe[0]
    • new SimpleXMLElement 带有匹配元素的子集:$sxe->ROWS, $sxe->{'ROWS'}
    • 迭代子项:foreach ($sxe as $e), $sxe->children()
    • 文本内容:(string) $sxe.SimpleXMLElement 总是返回另一个 SimpleXMLElement,所以如果你需要一个字符串显式转换它
    • Unprefixed child elements as numeric-index or traversable
      • Does not include prefixed elements (NOTE, I really mean prefixed, not null-namespace! SimpleXMLElement handling of namespaces is a strange and arguably broken.)
      • first child: $sxe[0]
      • new SimpleXMLElement with a subset of matching elements: $sxe->ROWS, $sxe->{'ROWS'}
      • iterate children: foreach ($sxe as $e), $sxe->children()
      • Text content: (string) $sxe. SimpleXMLElement always returns another SimpleXMLElement, so if you need a string cast it explicitly!
      • $sxe->children('http://example.org') 返回一个带有元素的新 SimpleXMLElement在匹配的命名空间中,删除命名空间,以便您可以像上一节一样使用它.
      • $sxe->children('http://example.org') returns a new SimpleXMLElement with elements in the matching namespace, with namespace stripped so you can use it like the previous section.
      • 特定属性:`$sxe['attribute-name']
      • 所有属性:$sxe->attributes()
      • $sxe->attributes() 返回一个特殊的 SimpleXMLElement,它将属性显示为 both 子元素 属性,因此以下两项都有效:
      • $sxe->attributes()->COMP_ID
      • $a = $sxe->attributes();$a['COMP_ID'];
      • 属性值:强制转换为字符串 (string) $sxe['attr-name']
      • specific attribute: `$sxe['attribute-name']
      • all attributes: $sxe->attributes()
      • $sxe->attributes() returns a special SimpleXMLElement that shows attributes as both child elements and attributes, so both the following work:
      • $sxe->attributes()->COMP_ID
      • $a = $sxe->attributes(); $a['COMP_ID'];
      • Value of an attribute: coerce to string (string) $sxe['attr-name']
      • 所有属性:$sxe->attributes('http://example.org')
      • 特定属性:$sxe_attrs = $sxe->attributes('http://example.org');$sxe_attrs['attr-name-without-prefix']

      你想要的是:

      $xml = '<ROOT><ROWS COMP_ID="165462"/><ROWS COMP_ID="165463"/></ROOT>';
      
      $sxe = simplexml_load_string($xml);
      
      foreach($sxe->ROWS as $row) {
          $id = (string) $row['COMP_ID'];
      }
      

      这篇关于循环访问 SimpleXMLElement 以访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:58