DOM - NamedNodeMap 对象方法 - getNamedItemNS

方法 getNamedItemNS () 检索由本地名称和命名空间 URI 指定的节点。

语法

以下是使用 getNamedItemNS() 方法的语法。

nodemapObject.getNamedItemNS(namespaceURI, localName);

S.No. 参数 &描述
1

namespaceURI

要检索的节点的 namespaceURI。其类型为 DOMString

2

localName

要检索的节点的本地名称。其类型为 DOMString

此方法返回指定节点的 namespaceURI 和本地名称,如果它们没有任何值,则返回 null。

示例

node_ns.xml 内容如下 −

<?xml version = "1.0"?>
<Company>
   <Employee  xmlns:e = "http://www.tutorials.com/technical/" category = "technical">
      <e:FirstName e:language = "English">Tanmay</e:FirstName>
      <e:LastName>Patil</e:LastName>
      <e:ContactNo>1234567890</e:ContactNo>
      <e:Email>tanmaypatil@xyz.com</e:Email>
   </Employee>

   <Employee xmlns:n = "http://www.tutorials.com/non-technical/" category = "non-technical">
      <n:FirstName>Taniya</n:FirstName>
      <n:LastName>Mishra</n:LastName>
      <n:ContactNo>1234667898</n:ContactNo>
      <n:Email>taniymishra@xyz.com</n:Email>
   </Employee>
</Company>

以下示例演示了 getNamedItemNS() 方法的用法 −

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
               xhttp = new XMLHttpRequest();
            } else // code for IE5 and IE6 {
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/dom/node_ns.xml");

         xmlDoc = xmlDoc.getElementsByTagName('e:FirstName')[0].attributes;
         document.write("Named Item Attribute node is :- ");
         document.write(xmlDoc.getNamedItemNS("http://www.tutorials.com/technical/",'language').nodeName);
         document.write("<br>");

         document.write("Named Item Attribute value is :- ");
         document.write(xmlDoc.getNamedItemNS("http://www.tutorials.com/technical/",'language').nodeValue);
      </script>
   </body>
</html>

执行

将此文件保存为服务器路径上的namednodemapmethod_getnameeditemns.html(此文件和node_ns.xml应位于服务器中的同一路径)。我们将获得如下所示的输出 −

Named Item Attribute node is :- e:language
Named Item Attribute value is :- English
dom_namednodemap_object.htm