DOM - Attribut d'objet NodeList - longueur

La longueur de l' attribut donne le nombre de nœuds dans la liste de nœuds.

Syntaxe

Voici la syntaxe pour l'utilisation de l' attribut length .

nodelistObject.length

Exemple

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

<?xml version = "1.0"?>
<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 analyse un document XML ( node.xml ) dans un objet DOM XML et extrait les informations de longueur à l'aide de l'attribut length.

<!DOCTYPE html>
   <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         y = xmlDoc.getElementsByTagName('FirstName');
         document.write("Length of node list: " + y.length);
      </script>
   </body>
</html>

Exécution

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

Length of node list: 3