Bloquée sur la mise à jour a partir d'un fichier txt et d'un fichier xml en java et Dom
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
Citation:
Jean Charles, 3214324565, 321, 2
Yvan Richard, 5435435545, 321, 1
Yvette Gagnon, 4324324243, 1, 12
voici le fichier inventaire.xml
Code:
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> |
et voici mon code
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
| 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...:ptdr:
Merci de ton temps et de ta patience
Citation:
Envoyé par
tsuji
Quoi ? comment ? Comment cela résulte en deux attributes nommés quantité ?
Il y a une error dans la ligne setAttribute() : elle manque un parenthèse "(" après le premier Integer.parseInt. Il ne compile pas, mais il ne résulte pas deux attributes quantité, c'est impossible.
voici la ligne de code
Code:
produit.setAttribute("quantite",(Integer.toString(Integer.parseInt(produit.getAttribute("quantité")) - (Integer.parseInt(str[3].trim())))));
et voici le résultat de l'execution
Code:
1 2 3 4 5
| <inventaire>
<produit code="1" prix="432.00" quantite="31" quantité="43"/>
<produit code="32" prix="32.00" quantité="100"/>
<produit code="321" prix="31.00" quantite="199" quantité="200"/>
</inventaire> |
il manque la première soustraction du code 321 qui est 2 et comme vous pouvez le constater
il ajout bien l'attribut quantité plutôt de la modifier :oops:
Merci encore de ton aide
probleme d'affichage, non resolu
Citation:
Envoyé par
emykev22
voici la ligne de code
Code:
produit.setAttribute("quantite",(Integer.toString(Integer.parseInt(produit.getAttribute("quantité")) - (Integer.parseInt(str[3].trim())))));
et voici le résultat de l'execution
Code:
1 2 3 4 5
| <inventaire>
<produit code="1" prix="432.00" quantite="31" quantité="43"/>
<produit code="32" prix="32.00" quantité="100"/>
<produit code="321" prix="31.00" quantite="199" quantité="200"/>
</inventaire> |
il manque la première soustraction du code 321 qui est 2 et comme vous pouvez le constater
il ajout bien l'attribut quantité plutôt de la modifier :oops:
Merci encore de ton aide