📜  PHP | DOMElement getElementsByTagNameNS()函数

📅  最后修改于: 2022-05-13 01:56:22.860000             🧑  作者: Mango

PHP | DOMElement getElementsByTagNameNS()函数

DOMElement::getElementsByTagNameNS()函数是PHP中的一个内置函数,用于获取具有给定 localName 和 namespaceURI 的所有后代元素。

句法:

DOMNodeList DOMElement::getElementsByTagNameNS( 
          string $namespaceURI, string $localName )

参数:此函数接受上面提到的两个参数,如下所述:

  • $namespaceURI:它指定命名空间 URI。
  • $localName:它指定本地名称。

返回值:此函数返回一个 DOMNodeList 值,该值包含所有匹配的元素,这些元素按照在此元素树的前序遍历中遇到的顺序排列。

下面给出的程序说明了PHP中的DOMElement::getElementsByTagNameNS()函数

方案一:

loadXML("

    
                     Hello, this is my red heading.                               Hello, this is my green heading.                               Hello, this is my blue heading.              
    
                     Hello, this is my new red heading.                               Hello, this is my new green heading.                               Hello, this is my new blue heading.              
");     // Get the elements with h1 tag from // specific namespace $nodeList = $dom->getElementsByTagNameNS(                     'my_namespace', 'h1');    foreach ($nodeList as $node) {     echo $node->getAttribute('x:style') . '
'; } ?>

输出:

color:red;color:green;color:blue;

方案二:

loadXML("

    
        HELLO.         NEW.         WORLD.     
    
        GEEKS         FOR         GEEKS     
");     // Get the elements $nodeList = $dom->getElementsByTagNameNS(                     'g4g_namespace', 'p');    foreach ($nodeList as $node) {     echo $node->textContent . '
'; } ?>

输出:

GEEKSFORGEEKS

参考: https://www. PHP.net/manual/en/domelement.getelementsbytagnamens。 PHP