I'm creating a java file that should update the values of a xml file from the values of a text file. In this case, the inventory should substract the quantity of each product that has been bought by a customer.
Text file : ( name, credit card number, product code, quantity )
Larry Lemans, 234234234, 45, 4
Gil Evans, 340934838, 45, 3
Bill Wong, 93823042, 54, 6
XML File :
<?xml version="1.0" encoding="ISO-8859-1"?>
<inventory>
<product code="54" price="432.00" quantity= "6" />
<product code="65" price="32.00" quantity= "8" />
<product code="45" price="31.00" quantity= "12" />
</inventory>
Desired XML File (after the update made by the java program) :
<?xml version="1.0" encoding="ISO-8859-1"?>
<inventory>
<product code="54" price="432.00" quantity= "0" />
<product code="65" price="32.00" quantity= "8" />
<product code="45" price="31.00" quantity= "5" />
</inventory>
Here's what I have so far :
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.BufferedReader;
import java.io.FileReader;
public class inventory {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("purchases.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (String str : values) {
UpdateInventory( purchase[2], purchase[3] );
Document doc = parser.parse("inventory.xml");
Element roote = doc.getDocumentElement();
NodeList nl = root.getChildNodes();
if (product.getAttribute("code").equals(code)){
product.setAttribute("quantity",Integer.toString
(Integer.parseInt(product.getAttribute("quantity")) - qte));
}
}
}
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("inventory.xml");
StreamResult result = new StreamResult(fw);
transformer.transform(source, result);
}
}
The big steps to achieve your desired result are:
Now you need to find ways to solve each one of these steps.