XML DOM - Rimuovi nodo

In questo capitolo, studieremo l' operazione di rimozione del nodo DOM XML . L'operazione di rimozione del nodo rimuove il nodo specificato dal documento. Questa operazione può essere implementata per rimuovere i nodi come il nodo di testo, il nodo dell'elemento o un nodo dell'attributo.

Di seguito sono riportati i metodi utilizzati per l'operazione di rimozione del nodo:

  • removeChild()

  • removeAttribute()

removeChild ()

Il metodo removeChild () rimuove il nodo figlio indicato da oldChild dall'elenco dei figli e lo restituisce. La rimozione di un nodo figlio equivale a rimuovere un nodo di testo. Quindi, la rimozione di un nodo figlio rimuove il nodo di testo ad esso associato.

Sintassi

La sintassi per utilizzare removeChild () è la seguente:

Node removeChild(Node oldChild) throws DOMException

Dove,

  • oldChild - è il nodo che viene rimosso.

  • Questo metodo restituisce il nodo rimosso.

Esempio: rimuovere il nodo corrente

Il seguente esempio (removecurrentnode_example.htm) analizza un documento XML ( node.xml ) in un oggetto DOM XML e rimuove il nodo specificato <ContactNo> dal nodo padre.

<!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.xml");

         document.write("<b>Before remove operation, total ContactNo elements: </b>");
         document.write(xmlDoc.getElementsByTagName("ContactNo").length);
         document.write("<br>");

         x = xmlDoc.getElementsByTagName("ContactNo")[0];
         x.parentNode.removeChild(x);

         document.write("<b>After remove operation, total ContactNo elements: </b>");
         document.write(xmlDoc.getElementsByTagName("ContactNo").length);
      </script>
   </body>
</html>

Nell'esempio sopra -

  • x = xmlDoc.getElementsByTagName ("ContactNo") [0] ottiene l'elemento <ContactNo> indicizzato a 0.

  • x.parentNode.removeChild (x); rimuove l'elemento <ContactNo> indicizzato a 0 dal nodo padre.

Esecuzione

Salva questo file come removecurrentnode_example.htm nel percorso del server (questo file e node.xml dovrebbero trovarsi sullo stesso percorso nel tuo server). Otteniamo il seguente risultato:

Before remove operation, total ContactNo elements: 3
After remove operation, total ContactNo elements: 2

Esempio: rimuovere il nodo di testo

Il seguente esempio (removetextNode_example.htm) analizza un documento XML ( node.xml ) in un oggetto DOM XML e rimuove il nodo figlio specificato <FirstName>.

<!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.xml");

         x = xmlDoc.getElementsByTagName("FirstName")[0];

         document.write("<b>Text node of child node before removal is:</b> ");
         document.write(x.childNodes.length);
         document.write("<br>");

         y = x.childNodes[0];
         x.removeChild(y);
         document.write("<b>Text node of child node after removal is:</b> ");
         document.write(x.childNodes.length);

      </script>
   </body>
</html>

Nell'esempio sopra -

  • x = xmlDoc.getElementsByTagName ("FirstName") [0]; - ottiene il primo elemento <FirstName> nella x indicizzata a 0.

  • y = x.childNodes [0]; - in questa riga y contiene il nodo figlio da rimuovere.

  • x.removeChild (y); - rimuove il nodo figlio specificato.

Esecuzione

Salva questo file come removetextNode_example.htm sul percorso del server (questo file e node.xml dovrebbero trovarsi sullo stesso percorso nel tuo server). Otteniamo il seguente risultato:

Text node of child node before removal is: 1
Text node of child node after removal is: 0

removeAttribute ()

Il metodo removeAttribute () rimuove un attributo di un elemento per nome.

Sintassi

La sintassi per utilizzare removeAttribute () è la seguente:

void removeAttribute(java.lang.String name) throws DOMException

Dove,

  • nome : è il nome dell'attributo da rimuovere.

Esempio

Il seguente esempio (removeelementattribute_example.htm) analizza un documento XML ( node.xml ) in un oggetto DOM XML e rimuove il nodo dell'attributo specificato.

<!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.xml");

         x = xmlDoc.getElementsByTagName('Employee');

         document.write(x[1].getAttribute('category'));
         document.write("<br>");

         x[1].removeAttribute('category');

         document.write(x[1].getAttribute('category'));

      </script>
   </body>
</html>

Nell'esempio sopra -

  • document.write (x [1] .getAttribute ('category')); - viene richiamato il valore della categoria di attributi indicizzata in prima posizione.

  • x [1] .removeAttribute ('category'); - rimuove il valore dell'attributo.

Esecuzione

Salva questo file come removeelementattribute_example.htm sul percorso del server (questo file e node.xml dovrebbero trovarsi sullo stesso percorso nel tuo server). Otteniamo il seguente risultato:

Non-Technical
null