NodeList
est live !org.w3c.dom.NodeList
. Document
dont est extraite la NodeList
.NodeList
pour, par exemple, supprimer
certains Elements
, par la méthode removeChild
de
l'interface Node
.Noms_notes
, on veut supprimer les éléments <eleve>
dont la note est inférieure à 5/20.Lire_1
(vue en cours) comme suit :Suppr.java
/* Noms-Notes en format 1 */
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class Suppr{
public static void suppr(Document doc, int barre)
throws Exception {
Element depart =
doc.getDocumentElement();
int k = 0; //nombre de
notes
NodeList les_eleves =
depart.getElementsByTagName("eleve");
for( int i = 0; i <
les_eleves.getLength(); i++ ){
Element l_eleve = (Element) les_eleves.item(i); // coercion !
String le_nom = l_eleve.getAttribute("nom");
String la_note = l_eleve.getAttribute("note");
if(
Integer.parseInt(la_note) < barre ){
l_eleve.getParentNode().removeChild(l_eleve);
System.out.println(le_nom+" a pour note "+la_note+ "
on le supprime de la liste.");
k++;
}//if
}// for
System.out.println("\nBarre
= "+barre+" : suppression de "+k+" noms-notes.");
}// suppr;
public static void main(String[] args) throws
Exception {
DocumentBuilder parseur =
DocumentBuilderFactory.
newInstance().newDocumentBuilder();
Document doc =
parseur.parse("NN1.xml");
suppr(doc, 5);
}//main
}// class Suppr
NN1.xml
<?xml version="1.0"
encoding="UTF-8"?> > |
Exécution :
Et pourtant il y en avait 7 à supprimer ! Où sont passés Elisabeth et Frédéric ? La suppression de Joseph a fait avancer Elisabeth à sa place, et au tour de boucle suivant on a trouvé Elsa au lieu d'Elisabeth... Idem pour Frédéric ! Car une NodeList est un objet vivant ... |
NodeList
interface to handle ordered lists of Nodes
, such as the
children of a Node
, or the elements returned by the Element.getElementsByTagNameNS(namespaceURI,
localName)
method, and also a NamedNodeMap
interface to handle unordered sets of nodes referenced by their name
attribute, such as the attributes of an Element
. NodeList
and NamedNodeMap
objects in the DOM are live;
that is, changes to the underlying document structure are reflected in
all relevant NodeList
and NamedNodeMap
objects. For example, if a DOM user gets a NodeList
object containing the children of an Element
, then
subsequently adds more children to that element (or removes children,
or modifies them), those changes are automatically reflected in the
NodeList
, without further action on the user's part.
Likewise, changes to a Node
in the tree are reflected in
all references to that Node
in NodeList
and
NamedNodeMap
objects.SupprA.java
if(
Integer.parseInt(la_note) < barre ){
l_eleve.getParentNode().removeChild(l_eleve);
System.out.println(le_nom+" a pour note "+la_note+ "
on le supprime de la liste.");
k++;
i--; // contrarie l'effet de l'effacement
sur la NodeList
}//if
NodeList
qui peut dépendre de l'implémentation...supprP.java
NodeList eleves =
depart.getElementsByTagName("eleve");
ArrayList<Element>
les_eleves = new ArrayList<Element>();
for( int i = 0; i <
eleves.getLength(); i++ ){
les_eleves.add( (Element) eleves.item(i) );
}// for i
for( Iterator<Element>
itr = les_eleves.iterator(); itr.hasNext(); ){
Element
l_eleve = itr.next();
String le_nom = l_eleve.getAttribute("nom");
String la_note = l_eleve.getAttribute("note");
...le
reste sans changement...
Suppr0.java
NodeList eleves =
depart.getElementsByTagName("eleve");
ArrayList<Element>
les_eleves = new ArrayList<Element>();
for( int i = 0; i <
eleves.getLength(); i++ ){
Element lel =
(Element) eleves.item(i);
if(
Integer.parseInt(lel.getAttribute("note")) < barre ){
les_eleves.add(lel);
}
}// for i
for( Iterator<Element> itr
= les_eleves.iterator(); itr.hasNext(); ){
Element l_eleve = itr.next();
String le_nom = l_eleve.getAttribute("nom");
String la_note = l_eleve.getAttribute("note");
l_eleve.getParentNode().removeChild(l_eleve);
System.out.println(le_nom+" a pour note "+la_note+ " on le supprime de
la liste.");
k++;
}// for itr