XML DOM getElementsByTagNameNS() Method

Οδηγός αναφοράς Document

Definition and Usage

The getElementsByTagNameNS() method can return a NodeList of all elements with a specified name and namespace.

Syntax:

getElementsByTagNameNS(ns,name)
parameters description
ns String value that can specify the namespace name. The value "*" can match all tags.
name String value that can specify the tag name to be searched. The value "*" can match all tags.

return value

Read-only array of Element nodes with specified namespace and local name in the document tree (technically, it is NodeList objects)。

πληροφορίες

Το πρόγραμμα getElementsByTagName() methodΠαρόμοια, αλλά αναζητά στοιχεία με βάση το όνομα χώρο και το όνομα. Χρησιμοποιείται μόνο σε αρχεία XML με όνομα χώρο.

παράδειγμα

Σε όλες τις παραδείξεις, θα χρησιμοποιήσουμε το αρχείο XML books.xml,κατάλληλο για JavaScript functions loadXMLDoc()

Η παρακάτω κωδικοποίηση μπορεί να προσθέσει ένα στοιχείο στοιχείου με όνομα χώρο σε κάθε στοιχείο <book>:

xmlDoc=loadXMLDoc("/example/xdom/books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel,newtext;
for (i=0;i<x.length;i++)
  {
  newel=xmlDoc.createElementNS('p','edition');
  newtext=xmlDoc.createTextNode('First');
  newel.appendChild(newtext);
  x[i].appendChild(newel);
  }
//Αποτύπωση όλων των title και έκδοση
var y=xmlDoc.getElementsByTagName("title");
var z=xmlDoc.getElementsByTagNameNS("p"," έκδοση");
for (i=0;i<y.length;i++)
  {
  document.write(y[i].childNodes[0].nodeValue);
  document.write(" - ");
  document.write(z[i].childNodes[0].nodeValue);
  document.write(" έκδοση");
  document.write("<br />");
  }

TIY

createElementNS() - Δημιουργία κόμβου με ονομαστικό χώρο(Δεν υποστηρίζεται από τον πρόγραμμα περιήγησης IE)

Οδηγός αναφοράς Document