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);
    }
}
edthatcherAsked:
Who is Participating?
 
for_yanCommented:
This compiles with some Product stub


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.padWithSpaces(p.getCode(), 8) +
            StringUtils.padWithSpaces(p.getDescription(), 44) +
            p.getFormattedPrice();

        System.out.println(productString);
    }
}

Open in new window

0
 
for_yanCommented:
usually hasNext()  upper case Next
0
 
for_yanCommented:
susually setCode() upper case Code
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
for_yanCommented:
and perhaps eventType - upper catse Type
0
 
for_yanCommented:
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
0
 
for_yanCommented:
Post Product class if you want me to check it more
0
 
JChrisonCommented:
switch (eventtype) Should be  switch (eventType)
0
 
JChrisonCommented:
and method hasnext() should be hasNext()
0
 
objectsCommented:
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);
    }
}

0
 
for_yanCommented:

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

0
 
edthatcherAuthor Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.