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

How to tell apart SimpleXML objects representing element and attribute?(如何区分表示元素和属性的 SimpleXML 对象?)
本文介绍了如何区分表示元素和属性的 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)
  • 这篇关于如何区分表示元素和属性的 SimpleXML 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)