Probleme modifier une balise avec DOM
Bonjour
j'ai un fichier du type:
Code:
1 2 3 4 5 6 7
| <accounts>
<account>
<LogonID>jsmith</LogonID>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</account>
</accounts> |
Je dois representer une base de donnee dans un fichier XML, et pouvoir executer des actions du type modifier firstName=toto où logonID a pour valeur jsmith
mon code est le suivant
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
public void modifyElement2(String xmlFile) throws SAXException, IOException, TransformerException, ParserConfigurationException {
File file = new File(xmlFile); // mon fichier xml contenant la database
System.out.println("1");
if (file.exists()){
// création d'une fabrique de documents
DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
// création d'un constructeur de documents
DocumentBuilder constructeur = fabrique.newDocumentBuilder();
// lecture du contenu d'un fichier XML avec DOM
File xml = new File(xmlFile);
Document doc = constructeur.parse(xml);
Vector <String> arrKeys=parserXml.getArrKeys();
String primaryKey =parserXml.getPrimaryKey();
String value = parserXml.getValuePrimaryKey();
NodeList listfind= doc.getElementsByTagName(primaryKey);
for(int j=0;j<listfind.getLength();j++){
Element e = (Element) listfind.item(j);
if (e.getTextContent().equals(value)){
Node node= e.getParentNode();
NodeList list= node.getChildNodes();
for(int i=0;i<list.getLength();i++){
Node f = list.item(i);
if (f.getNodeType()==Node.TEXT_NODE){
getModification(f);
}
}
}
}
}
}
public void getModification (Node node){
Iterator i = parserXml.getContent().keySet().iterator();
System.out.println("5");
while (i.hasNext())
{
String key = (String)i.next(); //la cle de modification
char firstletter=key.charAt(0);
int firstletterinASCII = (int) firstletter;
String value = (String)parserXml.getContent().get(key); // la valeur de modification
if (node.getNodeName().equals(key)){
node.setTextContent(value);}
// we change the attribute of the key
}
}
} |
mon code ne marche pas et ne maffiche rien
Mais je trouve ca bien long comme fonction pour faire ce aue je fais...Et je dosi imperativement utiliser DOM (fixe par le client)
Je ne comprends vrament pas , pouvez vous m'aider