Hi,

I am very new to both java and xml. I am trying to do the following :

I have a text file called “text.txt” containing the following information :


Code:
Brad Wood, 4562785341, 413, 3
Laury Mitchell, 7822345321, 413, 2
Jack Foster, 6454532347, 8, 13
On each line, I have : the name of the customer, the credit card number, the product number and the quantity purchased.

I also have an xml file called “inv.xml” containing the inventory.


Code:
<?xml version="1.0" encoding="UTF-8"?>
 <inventory>
 <product number="8" price="382.00" quantity= "24" />
 <product number="46" price="24.00" quantity= "85"  />
 <product number="413" price="12.00" quantity= "230"  />
 </inventory>
I need to build a java code that will update the inventory contained in the xml file depending on the purchases from the file “text.txt”. My java code is in a file called “InvUpdate.java” and the command “java InvUpdate text.txt inv.xml” need to modify the file “inv.xml” to subtract the quantity of product purchased. In this example, following the execution of the program the file “inv.xml” will contain the following information :

Code:
<?xml version="1.0" encoding="UTF-8"?>
 <inventory>
 <product number="8" price="382.00" quantity= "11" />
 <product number="46" price="24.00" quantity= "85"  />
 <product number="413" price="12.00" quantity= "225"  />
 </inventory>
The file “inv.xml” will always contain only one “product” element for each attribute value “number”. Also there is no invalid coding in the text file “text.txt”. I don’t know how many purchases there can be. It is possible to have a negative amount of product in the inventory.

I think it should be a pretty short program containing less than 50 lines. Also, I don’t need to use try-catch. As I said, I am a beginner and I am trying to learn, so please keep your answers very simple with basic java coding. No advanced coding please.

Here is what I did so far. I would like to keep my code pretty much as it is right now but just find a way to make it work together by adding what is missing or changing what is wrong. I understand there are many different ways to obtain the same result but I don't want to use coding that I haven't learned yet. As I said I am a beginner and I am trying to understand what I am doing. What you see below is pretty much an example of where I am at. Thank you for your help.

I am not sure how to code the "if" condition so it does the subtraction and write the changes in the xml file.


Code:
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class InvUpdate {
	
	public static void main(String[] args) throws IOException {  
        
		FileReader file = new FileReader("text.txt");  
        BufferedReader br = new BufferedReader(file);  
        String line = null;  
        
        while ((line = br.readLine()) != null) {    
        	String str[] =line.split(",");    
            System.out.println(str[2] +","+str[3]);   
        }  
        file.close();  
        
        DocumentBuilderFactory factory = 
        DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = 
        factory.newDocumentBuilder();
        Document doc = parser.parse(args[0]);
        Element root = doc.getDocumentElement();
        NodeList nl = root.getElementsByTagName("product");  
        
        
    	for (int i = 0; i < nl.getLength(); ++i) {	
    		Element product = (Element) nl.item(i);	
     
    if (product.getAttribute("number").equals(str[2])){  
    }  
    product.setAttribute("quantity",Integer.toString(Integer.parseInt(product.getAttribute("quantity")) - qty));

    	
    	}
    	
    TransformerFactory tfact = TransformerFactory.newInstance();
    Transformer transformer = tfact.newTransformer();
    transformer.setOutputProperty("encoding", "UTF-8");
    DOMSource source = new DOMSource(doc);
    FileWriter fw = new FileWriter("inventory.xml");
    StreamResult result = new StreamResult(fw);    
    transformer.transform(source, result);

	}
}