Link to home
Start Free TrialLog in
Avatar of edthatcher
edthatcher

asked on

Java code to read XML document

I am tyring to update the readProducts method so that is reads from the products.xml file but it wont compile.

these are the errors:
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exercises\java1.6\ch20\XMLTester\XMLTesterApp.java:49: cannot find symbol
symbol  : method hasnext()
location: interface javax.xml.stream.XMLStreamReader
        while (reader.hasnext())
                     ^
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exercises\java1.6\ch20\XMLTester\XMLTesterApp.java:52: cannot find symbol
symbol  : variable eventtype
location: class XMLTesterApp
                  switch (eventtype)
                          ^
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exercises\java1.6\ch20\XMLTester\XMLTesterApp.java:60: cannot find symbol
symbol  : method setcode(java.lang.String)
location: class Product
                              p.setcode(code);
                               ^
3 errors


Here is the code:
import java.util.ArrayList;
import java.io.*;
import javax.xml.stream.*;  // StAX API

public class XMLTesterApp
{
    private static String productsFilename = "products.xml";

    public static void main(String[] args)
    {
        System.out.println("Products list:");
        ArrayList<Product> products = readProducts();
        printProducts(products);

/*
        Product p1 = new Product("test", "XML Tester", 77.77);
        products.add(p1);
        writeProducts(products);
        System.out.println("XML Tester has been added to the XML document.\n");
*/

        System.out.println("Products list:");
        products = readProducts();
        printProducts(products);

/*
        products.remove(2);
        writeProducts(products);
        System.out.println("XML Tester has been deleted from the XML document.\n");
*/

        System.out.println("Products list:");
        products = readProducts();
        printProducts(products);

    }

    private static ArrayList<Product> readProducts()
    {
        ArrayList<Product> products = new ArrayList<Product>();
        Product p = null;

        // add code that reads the XML document from the products.xml file
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        FileReader fileReader = new FileReader("products.xml");
        XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader);


        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();
            }
          return products;
    }

    private static void writeProducts(ArrayList<Product> products)
    {
        // add code that writes the XML document to the products.xml file
    }

    private static void printProducts(ArrayList<Product> products)
    {
        for (Product p : products)
        {
            printProduct(p);
        }
        System.out.println();
    }

    private static void printProduct(Product p)
    {
        String productString =
            StringUtils.padWithSpaces(p.getCode(), 8) +
            StringUtils.padWithSpaces(p.getDescription(), 44) +
            p.getFormattedPrice();

        System.out.println(productString);
    }
}
Avatar of for_yan
for_yan
Flag of United States of America image

usually hasNext()  upper case Next
susually setCode() upper case Code
and perhaps eventType - upper catse Type
I don't have class Product to test

but hasnext() and evetType() I was right;

secode() is your own method from Product, so
you know better
Post Product class if you want me to check it more
switch (eventtype) Should be  switch (eventType)
and method hasnext() should be hasNext()
Avatar of Mick Barry
try this:


public class XMLTesterApp
{
    private static String productsFilename = "products.xml";

    public static void main(String[] args)
    {
        System.out.println("Products list:");
        ArrayList<Product> products = readProducts();
        printProducts(products);

/*
        Product p1 = new Product("test", "XML Tester", 77.77);
        products.add(p1);
        writeProducts(products);
        System.out.println("XML Tester has been added to the XML document.\n");
*/

        System.out.println("Products list:");
        products = readProducts();
        printProducts(products);

/*
        products.remove(2);
        writeProducts(products);
        System.out.println("XML Tester has been deleted from the XML document.\n");
*/

        System.out.println("Products list:");
        products = readProducts();
        printProducts(products);

    }

    private static ArrayList<Product> readProducts()
    {
        ArrayList<Product> products = new ArrayList<Product>();
        Product p = null;

        // add code that reads the XML document from the products.xml file
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        FileReader fileReader = new FileReader("products.xml");
        XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader);


        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();
            }
          return products;
    }

    private static void writeProducts(ArrayList<Product> products)
    {
        // add code that writes the XML document to the products.xml file
    }

    private static void printProducts(ArrayList<Product> products)
    {
        for (Product p : products)
        {
            printProduct(p);
        }
        System.out.println();
    }

    private static void printProduct(Product p)
    {
        String productString =
            StringUtils.padWithSpaces(p.getCode(), 8) +
            StringUtils.padWithSpaces(p.getDescription(), 44) +
            p.getFormattedPrice();

        System.out.println(productString);
    }
}

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

This is similar to above, but padWithSpaces() replaced by rightPad() method
and Product stub is attached below

import org.apache.commons.lang.StringUtils;

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

public class XMLTesterApp
{
    private static String productsFilename = "products.xml";

    public static void main(String[] args)
    {
        System.out.println("Products list:");
        ArrayList<Product> products = readProducts();
        printProducts(products);

/*
        Product p1 = new Product("test", "XML Tester", 77.77);
        products.add(p1);
        writeProducts(products);
        System.out.println("XML Tester has been added to the XML document.\n");
*/

        System.out.println("Products list:");
        products = readProducts();
        printProducts(products);

/*
        products.remove(2);
        writeProducts(products);
        System.out.println("XML Tester has been deleted from the XML document.\n");
*/

        System.out.println("Products list:");
        products = readProducts();
        printProducts(products);

    }

    private static ArrayList<Product> readProducts()
    {
        ArrayList<Product> products = new ArrayList<Product>();
        Product p = null;

        try {

        // add code that reads the XML document from the products.xml file
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        FileReader fileReader = new FileReader("products.xml");
        XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader);


        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(Exception ex){
            System.out.println("error " + ex.toString());
            ex.printStackTrace();
        }
         return products;
    }

    private static void writeProducts(ArrayList<Product> products)
    {
        // add code that writes the XML document to the products.xml file
    }

    private static void printProducts(ArrayList<Product> products)
    {
        for (Product p : products)
        {
            printProduct(p);
        }
        System.out.println();
    }

    private static void printProduct(Product p)
    {
        String productString =
            StringUtils.rightPad(p.getCode(), 8) +
            StringUtils.rightPad(p.getDescription(), 44) +
            p.getFormattedPrice();

        System.out.println(productString);
    }
}

class Product{
    public void setCode(String s){

    }
      public void setDescription(String s){

    }
        public void setPrice(double d){

    }

      public String getCode(){
          return null;

    }
       public String getDescription(){
                return null;
    }

    public double getFormattedPrice(){
          return 0.0;
    }
}

Open in new window

Avatar of edthatcher
edthatcher

ASKER

thank you