如何区分表示元素和属性的SimpleXML对象? [英] How to tell apart SimpleXML objects representing element and attribute?

查看:69
本文介绍了如何区分表示元素和属性的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)    |
+------------------+---------------------------------------------+

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆