如何区分表示元素和属性的 SimpleXML 对象?

2021-12-26 00:00:00 reflection php simplexml

我需要以特定方式打印任意 SimpleXML 对象,并对属性节点进行特殊处理.

I need to print arbitrary SimpleXML objects in a specific manner, with special handling of attribute nodes.

问题在于 SimpleXML 元素和属性似乎使用完全相同的类,属性节点甚至假装支持 attributes() 方法,而 SimpleXML 隐藏了其内部结构,因此似乎没有可以通过任何方式告诉节点类型(除了生成 XML 并重新解析它).

The problem is that SimpleXML elements and attributes seem to use exactly the same class, attribute node even pretends to support attributes() method, and SimpleXML hides its internals, so there doesn't seem to be any way to tell type of node (short of generating XML and reparsing it).

两者都给出相同的结果:

$element = new SimpleXMLElement('<foo>test</foo>');
echo $element;
print_r($element);

$element = new SimpleXMLElement('<foo attr="test" />');
echo $element['attr'];
print_r($element['attr']);

是否有允许在 SimpleXML 中识别节点类型的隐藏属性/方法?等效于 DOM 的 $node->nodeType$node instanceof DOMAttr?(我不能用 DOM 代替,支持 SimpleXML 是核心要求).

Is there a hidden property/method that allows identifying type of node in SimpleXML? Equivalent of DOM's $node->nodeType or $node instanceof DOMAttr? (I can't use DOM instead, support for SimpleXML is core requirement).

推荐答案

SimpleXMLElement 中没有内置属性可以让您区分这些.

There are no built-in properties in SimpleXMLElement which would allow you to tell these apart.

正如其他人所建议的 dom_import_simplexml 可能是合适的,但是,该功能可以改变有时,节点是动态的,例如,如果您传入子节点或命名子节点的列表,它会将它们转换为第一个元素.

As others have suggested dom_import_simplexml can be appropriate, however, that function can change nodes on the fly sometimes, for example, if you pass in a list of childnodes or named childnodes, it will take those and turn them into the first element.

如果它是一个空列表,例如没有从 attributes() 返回的属性或不存在的命名子节点,它会给出一个警告,告诉你一个无效的节点类型已经给出:

If it's an empty list, for example no attributes returned from attributes() or non-existing named childnodes, it will give a warning telling you an invalid nodetype has been given:

警告:dom_import_simplexml():要导入的节点类型无效

Warning: dom_import_simplexml(): Invalid Nodetype to import

因此,如果您需要使用活泼的布尔值 true/false 进行精确处理,请使用 Simplexml:

So if you need this precise with a snappy boolean true/false, here is how it works with Simplexml:

$isElement   = $element->xpath('.') == array($element);

$isAttribute = $element[0] == $element
               and $element->xpath('.') != array($element);

它与属性列表和元素列表类似,我只是早上写了一篇关于这个的博客,你需要有一些关于评估什么的具体知识,所以我为它创建了一个备忘单:

It works similar with attribute lists and element lists, I've just blogged about this in the morning, you need to have some specific knowledge about what to evaluate for what, so I created a cheatsheet for it:

+------------------+---------------------------------------------+
| TYPE             | TEST                                        |
+------------------+---------------------------------------------+
| Element          | $element->xpath('.') == array($element)     |
+------------------+---------------------------------------------+
| Attribute        | $element[0] == $element                     |
|                  | and $element->xpath('.') != array($element) |
+------------------+---------------------------------------------+
| Attributes       | $element->attributes() === NULL             |
+------------------+---------------------------------------------+
| Elements         | $element[0] != $element                     |
|                  | and $element->attributes() !== NULL         |
+------------------+---------------------------------------------+
| Single           | $element[0] == $element                     |
+------------------+---------------------------------------------+
| Empty List       | $element[0] == NULL                         |
+------------------+---------------------------------------------+
| Document Element | $element->xpath('/*') == array($element)    |
+------------------+---------------------------------------------+

  • SimpleXML 类型备忘单(2013 年 2 月 12 日;作者:hakre)

相关文章