CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

XML

XML
StAX
Class that works with an XML file

The code for the ProductXMLFile class

import java.util.*;
import java.io.*;
import javax.xml.stream.*;  // StAX API

public class ProductXMLFile implements ProductDAO
{
    private String productsFilename = "products.xml";
    private File productsFile = null;

    public ProductXMLFile()
    {
        productsFile = new File(productsFilename);
    }

    private void checkFile() throws IOException
    {
        // if the file doesn't exist, create it
        if (!productsFile.exists())
            productsFile.createNewFile();
    }
    private boolean saveProducts(
        ArrayList products)
    {
        // create the XMLOutputFactory object
        XMLOutputFactory outputFactory =
            XMLOutputFactory.newInstance();

        try
        {
            // check the file to make sure it exists
            this.checkFile();

            // create XMLStreamWriter object
            FileWriter fileWriter =
                new FileWriter(productsFilename);
            XMLStreamWriter writer =
                outputFactory.createXMLStreamWriter(
                    fileWriter);
            writer.writeEndElement();
            writer.writeEndDocument();
            writer.flush();
            writer.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return false;
        }
        catch (XMLStreamException e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public ArrayList getProducts()
    {
        ArrayList products =
            new ArrayList();
        Product p = null;

        // create the XMLInputFactory object
        XMLInputFactory inputFactory =
            XMLInputFactory.newInstance();

        try
        {
            // check the file to make sure it exists
            this.checkFile();

            // create a XMLStreamReader object
            FileReader fileReader =
                new FileReader(productsFilename);
            XMLStreamReader reader =
                inputFactory.createXMLStreamReader(
                    fileReader);
            // read the products from the file
            while (reader.hasNext())
            {
                int eventType = reader.getEventType();
                switch (eventType)
                {
                    case XMLStreamConstants.START_ELEMENT:
                        String elementName =
                            reader.getLocalName();
                        if (elementName.equals("Product"))
                        {
                            p = new Product();

                            String code =
                                reader.getAttributeValue(0);
                            p.setCode(code);
                        }
                        if (elementName.equals(
                            "Description"))
                        {
                            String description =
                                reader.getElementText();
                            p.setDescription(description);
                        }
                        if (elementName.equals("Price"))
                        {
                            String priceString =
                                reader.getElementText();
                            double price =
                            Double.parseDouble(priceString);
                            p.setPrice(price);
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        elementName = reader.getLocalName();
                        if (elementName.equals("Product"))
                        {
                            products.add(p);
                        }
                        break;
                    default:
                        break;
                }
                reader.next();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
		catch (XMLStreamException e)
        {
            e.printStackTrace();
            return null;
        }
        return products;
    }

    public Product getProduct(String code)
    {
        ArrayList products = this.getProducts();
        for (Product p : products)
        {
            if (p.getCode().equals(code))
                return p;
        }
        return null;
    }
    public boolean addProduct(Product p)
    {
        ArrayList products = this.getProducts();
        products.add(p);
        return this.saveProducts(products);
    }

    public boolean deleteProduct(Product p)
    {
        ArrayList products = this.getProducts();
        products.remove(p);
        return this.saveProducts(products);
    }

	public boolean updateProduct(Product newProduct)
    {
        ArrayList products = this.getProducts();

        // get the old product and remove it
        Product oldProduct =
            this.getProduct(newProduct.getCode());
        int i = products.indexOf(oldProduct);
        products.remove(i);

        // add the updated product
        products.add(i, newProduct);

        return this.saveProducts(products);
    }
}
Previous | Create XMLStreamWriter object | Write XML | Create XMLStreamReader object | Read XML | Class that works with an XML file