DOM - Méthode d'objet NamedNodeMap - getNamedItem

La méthode getNamedItem () récupère le nœud spécifié par son nom.

Syntaxe

Voici la syntaxe pour l'utilisation de la méthode getNamedItem () .

nodemapObject.getNamedItem(name)

S.No. Paramètre et description
1

name

Cela spécifie le nom du nœud à récupérer. Il est de type DOMString .

Cette méthode renvoie le nœud spécifié par son nom.

Exemple

Le contenu de node.xml est comme ci-dessous -

<Company>
   <Employee category = "Technical">
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>[email protected]</Email>
   </Employee>
   
   <Employee category = "Non-Technical">
      <FirstName>Taniya</FirstName>
      <LastName>Mishra</LastName>
      <ContactNo>1234667898</ContactNo>
      <Email>[email protected]</Email>
   </Employee>
   
   <Employee category = "Management">
      <FirstName>Tanisha</FirstName>
      <LastName>Sharma</LastName>
      <ContactNo>1234562350</ContactNo>
      <Email>[email protected]</Email>
   </Employee>
</Company>

L'exemple suivant montre l'utilisation de la méthode getNamedItem () -

<!DOCTYPE html>
<html>
   <body>
      <script>
         if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         xmlDoc = xmlDoc.getElementsByTagName('Employee')[0].attributes;
         document.write("Name of attribute category for node Employee is: ");
         document.write(xmlDoc.getNamedItem('category').nodeValue);
     </script>
   </body>
</html>

Exécution

Enregistrez ce fichier sous le nom namednodemapmethod_getnameditem.htm sur le chemin du serveur (ce fichier et node_methods.xml doivent être sur le même chemin sur votre serveur). Nous obtiendrons la sortie comme indiqué ci-dessous -

Name of attribute category for node Employee is: Technical