有没有办法获得所有的DOMElement的属性? [英] Is there a way to get all of a DOMElement's attributes?
问题描述
我正在使用PHP阅读一些XML,目前正在使用 DOMDocument
类来实现。我需要一种方法来获取标签的名称和值( DOMElement
)的属性,而不需要事先知道它们中的任何一个。 文档似乎没有提供任何这样的东西。我知道我可以得到一个属性的价值,如果我有它的名字,但再次,我不知道这些,并需要找到这两个。
I'm reading some XML with PHP and currently using the DOMDocument
class to do so. I need a way to grab the names and values of a tag's (instance of DOMElement
) attributes, without knowing beforehand what any of them are. The documentation doesn't seem to offer anything like this. I know that I can get an attribute's value if I have its name, but again, I don't know either of these and need to find both.
我也知道其他类,如 SimpleXMLElement
具有此功能,但我对如何使用 DOMDocument
/ p>
I also know that other classes like SimpleXMLElement
have this capability, but I'm interested in how it can be done with DOMDocument
.
推荐答案
您可以使用 DomNode->属性属性,它会返回你 DOMNamedNodeMap ,其中包含属性名称和值。
You can get all the attributes of a given DomNode, using the DomNode->attributes property, it will return you DOMNamedNodeMap containing the attribute names and values.
foreach ($node->attributes as $attrName => $attrNode) {
// ...
}
这篇关于有没有办法获得所有的DOMElement的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!