問題描述
我需要以特定方式打印任意 SimpleXML 對象,并對屬性節(jié)點進行特殊處理.
I need to print arbitrary SimpleXML objects in a specific manner, with special handling of attribute nodes.
問題在于 SimpleXML 元素和屬性似乎使用完全相同的類,屬性節(jié)點甚至假裝支持 attributes()
方法,而 SimpleXML 隱藏了其內(nèi)部結(jié)構(gòu),因此似乎沒有可以通過任何方式告訴節(jié)點類型(除了生成 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).
兩者都給出相同的結(jié)果:
$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 中識別節(jié)點類型的隱藏屬性/方法?等效于 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
中沒有內(nèi)置屬性可以讓您區(qū)分這些.
There are no built-in properties in SimpleXMLElement
which would allow you to tell these apart.
正如其他人所建議的 dom_import_simplexml
可能是合適的,但是,該功能可以改變有時,節(jié)點是動態(tài)的,例如,如果您傳入子節(jié)點或命名子節(jié)點的列表,它會將它們轉(zhuǎn)換為第一個元素.
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()
返回的屬性或不存在的命名子節(jié)點,它會給出一個警告,告訴你一個無效的節(jié)點類型已經(jīng)給出:
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():要導(dǎo)入的節(jié)點類型無效
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);
它與屬性列表和元素列表類似,我只是早上寫了一篇關(guān)于這個的博客,你需要有一些關(guān)于評估什么的具體知識,所以我為它創(chuàng)建了一個備忘單:
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)
這篇關(guān)于如何區(qū)分表示元素和屬性的 SimpleXML 對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!