简体   繁体   中英

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

An arraylist would just be fantastic. I think @Bruno may try testing java's containers a bit.

Well, you could take a gander at the classes for the purpose in Guava.

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#readLines(java.io.File,%20java.nio.charset.Charset)

is a method you can call, passing it a file, and receive in return a list of Strings, one per line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM