【问题标题】:Is there a way to get all of a DOMElement's attributes?有没有办法获取 DOMElement 的所有属性?
【发布时间】:2010-11-23 05:55:37
【问题描述】:

我正在使用 PHP 读取一些 XML,目前正在使用 DOMDocument 类来执行此操作。我需要一种方法来获取标签(DOMElement 的实例)属性的名称和值,而无需事先知道它们是什么。 documentation 似乎没有提供这样的东西。我知道如果我有它的名称,我可以得到一个属性的值,但同样,我不知道其中任何一个,需要找到两者。

我也知道像 SimpleXMLElement 这样的其他类也有这种能力,但我对如何使用 DOMDocument 来完成它很感兴趣。

【问题讨论】:

    标签: php xml dom domdocument


    【解决方案1】:

    我在寻找将节点属性转换为数组以便将该数组与数据库中的结果进行比较的方法时偶然发现了这个问题。来自https://stackoverflow.com/users/264502/jan-molak 的回答确实可以解决问题,但就我而言,它并没有说明节点中可能缺少某些属性或者它们可能是空字符串,而从 DB 返回了 NULLs 的事实。
    为了涵盖这一点,我已将其扩展为以下功能,这可能对其他人也有用:

        #Function to convert DOMNode into array with set of attributes, present in the node
        #$null will replace empty strings with NULL, if set to true
        #$extraAttributes will add any missing attributes as NULL or empty strings. Useful for standartization
        public function attributesToArray(\DOMNode $node, bool $null = true, array $extraAttributes = []): array
        {
            $result = [];
            #Iterrate attributes of the node
            foreach ($node->attributes as $attrName => $attrValue) {
                if ($null && $attrValue === '') {
                    #Add to resulting array as NULL, if it's empty string
                    $result[$attrName] = NULL;
                } else {
                    #Add actual value
                    $result[$attrName] = $attrValue->textContent;
                }
            }
            #Add any additional attributes, that are expected
            if (!empty($extraAttributes)) {
                foreach ($extraAttributes as $attribute) {
                    if (!isset($result[$attribute])) {
                        if ($null) {
                            #Add as NULL
                            $result[$attribute] = NULL;
                        } else {
                            #Or add as empty string
                            $result[$attribute] = '';
                        }
                    }
                }
            }
            #Return resulting string
            return $result;
        }
    }
    

    我已将 nodeValue 替换为 textContent,因为在谈到属性时,我觉得它更“自然”一些,但从技术上讲,无论如何它们在这里都是一样的。
    如果需要,此功能可在 Composer 中作为 Simbiat/ArrayHelpers (https://github.com/Simbiat/ArrayHelpers) 的一部分使用

    【讨论】:

      【解决方案2】:

      如果你想获取属性名和属性值(不是attributeNodes)你必须调用DOMNode对象的$attrNode->nodeValue属性。

      $attributes = array();
      
      foreach($element->attributes as $attribute_name => $attribute_node)
      {
        /** @var  DOMNode    $attribute_node */
        $attributes[$attribute_name] = $attribute_node->nodeValue;
      }
      

      【讨论】:

      • 这个答案比较完整。
      【解决方案3】:

      您可以使用DomNode->attributes 属性获取给定DomNode 的所有属性,它会返回包含属性名称和值的DOMNamedNodeMap

      foreach ($node->attributes as $attrName => $attrNode) {
          // ...
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2020-05-01
      • 2016-08-20
      • 2017-06-15
      • 2011-03-29
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多