Java - XML

Reading & Writing to an XML file.

Cars / XML

package ws;

import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import model.Cars;
import model.Inventory;

public class CarsXML {
    ////////////////////////////////////////////////////////////
    // ABSOLUTE PATH NEEDS TO CHANGE IN ORDER TO RUN PROPERLY //
    //   REMINDER TO CHANGE TO C:\CARS_DATA.XML FOR COLIN     //
    ////////////////////////////////////////////////////////////
    private final String filename = "D:\\car_data.xml";
    private Inventory inventory;

    public CarsXML(){
        this.inventory = new Inventory();
        inventory.setList((parseCars()));
    }

    public ArrayList<Cars> getCars(){
        return inventory.getInventory();
    }

    public void add(Cars newCar){
        getCars().add(new Cars(newCar.getModel(), newCar.getType(), newCar.getPrice())); 
        rewrite();
    }

    public void delete(int id){
        Cars car = null;
        for (Cars c : getCars())
            if (c.getId() == id)
                car = c;

        if (car != null)
            getCars().remove(car);
        rewrite();
    }

    public void update(int id, int price){
        for (Cars c : getCars())
            if (c.getId() == id){
                c.setPrice(price);
                break;
            }
        rewrite();
    }

    private ArrayList<Cars> parseCars(){
        ArrayList<Cars> cars = new ArrayList<>();
        try{
            File file = new File(filename);
            JAXBContext jaxbContext = JAXBContext.newInstance(Inventory.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            cars = ((Inventory) jaxbUnmarshaller.unmarshal(file)).getInventory();
        }
        catch (JAXBException ex){ 
            System.out.println("Parse function error.");
            Logger.getLogger(CarsXML.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cars;
    }

    public void rewrite(){
        try{
            File file = new File(filename);
            JAXBContext jaxbContext = JAXBContext.newInstance(Inventory.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(inventory, file);
        }
        catch (JAXBException ex){
            System.out.println("Rewrite function error.");
            Logger.getLogger(CarsXML.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}