Suppression d'un élément dans un fichier XML
Salut à tous!
J'aimerais créer une méthode pour supprimer un élément dans un fichier XML. En fait, je voudrais supprimer un élément particulier, càd, celui dont l'attribut "name" correspond au String elt.
Le problème est que j'utilise le type "Element" pour faire la recherche du bon élément, et le type "Node" pour réaliser la suppression.
Je ne connais pas d'autres méthodes... :oops:
Voici ma méthode JAVA:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
public void removeElt(String elt){
NodeList list= document.getElementsByTagName("uIndex");
int nb= list.getLength();
int i=0;
while (i<nb){
Node child = list.item(i);
Element childElt = (Element)child;
String atr = childElt.getAttribute("name");
if (atr.equals(elt)){
root.removeChild(child);
}
i=i+1;
}
save();
} |
et mon fichier XML:
Code:
1 2 3 4 5 6 7 8 9 10
|
<uIndexList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./xsd/uIndexList.xsd">
<uIndex name="un"/>
<uIndex name="coucou"/>
<uIndex name="salut"/>
<uIndex name="bijour"/>
<uIndex name="hello"/>
<uIndex name="rigole"/>
<uIndex name="ralout"/>
</uIndexList> |
Merci d'avance!