<small id='VPVz9'></small><noframes id='VPVz9'>

      <bdo id='VPVz9'></bdo><ul id='VPVz9'></ul>

    <legend id='VPVz9'><style id='VPVz9'><dir id='VPVz9'><q id='VPVz9'></q></dir></style></legend>

  1. <tfoot id='VPVz9'></tfoot>

      <i id='VPVz9'><tr id='VPVz9'><dt id='VPVz9'><q id='VPVz9'><span id='VPVz9'><b id='VPVz9'><form id='VPVz9'><ins id='VPVz9'></ins><ul id='VPVz9'></ul><sub id='VPVz9'></sub></form><legend id='VPVz9'></legend><bdo id='VPVz9'><pre id='VPVz9'><center id='VPVz9'></center></pre></bdo></b><th id='VPVz9'></th></span></q></dt></tr></i><div id='VPVz9'><tfoot id='VPVz9'></tfoot><dl id='VPVz9'><fieldset id='VPVz9'></fieldset></dl></div>
    1. 如何区分表示元素和属性的 SimpleXML 对象?

      时间:2023-09-22
      <i id='yTfIE'><tr id='yTfIE'><dt id='yTfIE'><q id='yTfIE'><span id='yTfIE'><b id='yTfIE'><form id='yTfIE'><ins id='yTfIE'></ins><ul id='yTfIE'></ul><sub id='yTfIE'></sub></form><legend id='yTfIE'></legend><bdo id='yTfIE'><pre id='yTfIE'><center id='yTfIE'></center></pre></bdo></b><th id='yTfIE'></th></span></q></dt></tr></i><div id='yTfIE'><tfoot id='yTfIE'></tfoot><dl id='yTfIE'><fieldset id='yTfIE'></fieldset></dl></div>

            <tbody id='yTfIE'></tbody>
          <legend id='yTfIE'><style id='yTfIE'><dir id='yTfIE'><q id='yTfIE'></q></dir></style></legend>

            <tfoot id='yTfIE'></tfoot>

            <small id='yTfIE'></small><noframes id='yTfIE'>

              • <bdo id='yTfIE'></bdo><ul id='yTfIE'></ul>
                本文介绍了如何区分表示元素和属性的 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 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PHP 5 反射 API 性能 下一篇:警告“不要直接访问超全局 $_POST 数组"在 N

                相关文章

                最新文章

                <small id='EisWp'></small><noframes id='EisWp'>

                1. <tfoot id='EisWp'></tfoot>
                  <i id='EisWp'><tr id='EisWp'><dt id='EisWp'><q id='EisWp'><span id='EisWp'><b id='EisWp'><form id='EisWp'><ins id='EisWp'></ins><ul id='EisWp'></ul><sub id='EisWp'></sub></form><legend id='EisWp'></legend><bdo id='EisWp'><pre id='EisWp'><center id='EisWp'></center></pre></bdo></b><th id='EisWp'></th></span></q></dt></tr></i><div id='EisWp'><tfoot id='EisWp'></tfoot><dl id='EisWp'><fieldset id='EisWp'></fieldset></dl></div>

                  <legend id='EisWp'><style id='EisWp'><dir id='EisWp'><q id='EisWp'></q></dir></style></legend>
                    <bdo id='EisWp'></bdo><ul id='EisWp'></ul>