简体   繁体   English

java从.txt文件更新.XML文件

[英]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. 每次购买时,我都有一个“ achat.txt”文件,并用逗号分隔。

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

I have a inventaire.XML file my inventory. 我的库存中有一个inventaire.XML文件。

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. 我必须编写一个程序,其中DOM考虑“ achats.txt”来更新清单。文件更新仅适用于“ achats.txt”文件的属性“ code”和“ quantity”。

I managed to read the "achats.txt" file in java program. 我设法在Java程序中读取“ achats.txt”文件。

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. 我不知道如何创建包含变量achats.txt文件的字符串数组。 I don't know either how to get the "code" and "quantity" attribute then subtract the inventaire.xml file. 我也不知道如何获取“代码”和“数量”属性,然后减去inventaire.xml文件。 Thank you for your help 谢谢您的帮助

An arraylist would just be fantastic. 一个数组列表将是太棒了。 I think @Bruno may try testing java's containers a bit. 我认为@Bruno可能会尝试测试Java的容器。

Well, you could take a gander at the classes for the purpose in Guava. 好吧,您可以在Guava为此目的参加课程。

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#readLines(java.io.File,%20java.nio.charset.Charset) 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. 是一种可以调用的方法,将其传递给文件,然后以返回形式接收字符串列表,每行一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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