java Update a .XML file from a .txt file



I have a "achat.txt" file on every purchase with a line separated by commas information.



Jean Charles, 3214324565, 321, 2
Yvan Richard, 5435435545, 321, 1
Yvette Gagnon, 4324324243, 1, 12


I have a inventaire.XML file my inventory.


Code :



<?xml version="1.0" encoding="ISO-8859-1"?>
<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>


I have to write a program in which DOM updates the inventory taking into account the "achats.txt" .The file update only applies to the attribute "code" and "quantity" of "achats.txt" file.


I managed to read the "achats.txt" file in java program.


Code :



import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;




public class Affiche { // On créer une classe "Affiche"
public static void main(String[] args) throws IOException {
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]);


}
fichier.close()
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse("inventaire.xml");
Element racine = doc.getDocumentElement();
NodeList nl = racine.getElementsByTagName("produit");
for (int i = 0; i < nl.getLength(); ++i) {
Element produit = (Element) nl.item(i);
}


I don't know how to create an array of string that will contain the variables achats.txt file. I don't know either how to get the "code" and "quantity" attribute then subtract the inventaire.xml file. Thank you for your help


No comments:

Post a Comment