Bonjour a vous tous!
Je doit mette à jour un fichier inventaire.xml a partir d'un fichier Achats.txt
Mais je ne réussi pas a entre dans ma boucle et faire la modification
pouriez-vous m'aider a trouver le problème
merci...
voici le fichier Achats.txt
voici le fichier inventaire.xmlJean Charles, 3214324565, 321, 2
Yvan Richard, 5435435545, 321, 1
Yvette Gagnon, 4324324243, 1, 12
et voici mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <inventaire> <produit code="1" prix="432.00" quantité="43"/> <produit code="32" prix="32.00" quantité="100"/> <produit code="321" prix="31.00" quantité="200"/> </inventaire>
Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class inventaire6 { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); Document doc = parser.parse("inventaire.xml"); Element racine = doc.getDocumentElement(); NodeList nl = racine.getChildNodes(); FileReader fichier = new FileReader("achats.txt"); BufferedReader br = new BufferedReader(fichier); String ligne = null; while ((ligne = br.readLine()) != null) { String str[] =ligne.split(","); System.out.println(str[2] +","+str[3]); NodeList n2 = racine.getElementsByTagName("produit"); for (int i = 0; i < n2.getLength(); ++i) { Element produit = (Element) n2.item(i); System.out.println(produit.getTagName()); // ### ICI LE CODE QUI POSE PROBLÈME à mon avis if (produit.getAttribute("code").equals(str[2])) { System.out.println(str[2] +","+str[3]); produit.setAttribute("quantite",Integer.toString(Integer.parseInt produit.getAttribute("quantité")) - (Integer.parseInt(str[3])))); } // ### FIN } } br.close(); TransformerFactory tfact = TransformerFactory.newInstance(); Transformer transformer = tfact.newTransformer(); transformer.setOutputProperty("encoding", "ISO-8859-1"); DOMSource source = new DOMSource(doc); FileWriter fw = new FileWriter("inventaire.xml"); StreamResult result = new StreamResult(fw); transformer.transform(source, result); }}
J'ai indiqué le code qui d'après moi cause problème
Merci encore de votre temps...
Partager