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\Exe rcises\jav a1.6\ch20\ XMLTester\ XMLTesterA pp.java:49 : cannot find symbol
symbol : method hasnext()
location: interface javax.xml.stream.XMLStream Reader
while (reader.hasnext())
^
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exe rcises\jav a1.6\ch20\ XMLTester\ XMLTesterA pp.java:52 : cannot find symbol
symbol : variable eventtype
location: class XMLTesterApp
switch (eventtype)
^
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exe rcises\jav a1.6\ch20\ XMLTester\ XMLTesterA pp.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("Produc ts 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("Produc ts 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("Produc ts 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.newInstanc e();
FileReader fileReader = new FileReader("products.xml") ;
XMLStreamReader reader = inputFactory.createXMLStre amReader(f ileReader) ;
while (reader.hasnext())
{
int eventType = reader.getEventType();
switch (eventtype)
{
case XMLStreamConstants.START_E LEMENT:
String elementName = reader.getLocalName();
if (elementName.equals ("Product"))
{
p = new Product();
String code = reader.getAttributeValue(0 );
p.setcode(code);
}
if (elementName.equals("Descr iption"))
{
String description = reader.getElementText();
p.setDescription(descripti on);
}
if (elementName.equals("Price "))
{
String priceString = reader.getElementText();
double price = Double.parseDouble(priceSt ring);
p.setPrice(price);
}
break;
case XMLStreamConstants.END_ELE MENT:
elementName = reader.getLocalName();
if (elementName.equals("Produ ct"))
{
products.add(p);
}
break;
default:
break;
}
reader.next();
}
return products;
}
private static void writeProducts(ArrayList<Pr oduct> products)
{
// add code that writes the XML document to the products.xml file
}
private static void printProducts(ArrayList<Pr oduct> 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.getDescr iption(), 44) +
p.getFormattedPrice();
System.out.println(product String);
}
}
these are the errors:
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exe
symbol : method hasnext()
location: interface javax.xml.stream.XMLStream
while (reader.hasnext())
^
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exe
symbol : variable eventtype
location: class XMLTesterApp
switch (eventtype)
^
C:\Documents and Settings\ethatc001\My Documents\Murach\Java6\Exe
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("Produc
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("Produc
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("Produc
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.newInstanc
FileReader fileReader = new FileReader("products.xml")
XMLStreamReader reader = inputFactory.createXMLStre
while (reader.hasnext())
{
int eventType = reader.getEventType();
switch (eventtype)
{
case XMLStreamConstants.START_E
String elementName = reader.getLocalName();
if (elementName.equals ("Product"))
{
p = new Product();
String code = reader.getAttributeValue(0
p.setcode(code);
}
if (elementName.equals("Descr
{
String description = reader.getElementText();
p.setDescription(descripti
}
if (elementName.equals("Price
{
String priceString = reader.getElementText();
double price = Double.parseDouble(priceSt
p.setPrice(price);
}
break;
case XMLStreamConstants.END_ELE
elementName = reader.getLocalName();
if (elementName.equals("Produ
{
products.add(p);
}
break;
default:
break;
}
reader.next();
}
return products;
}
private static void writeProducts(ArrayList<Pr
{
// add code that writes the XML document to the products.xml file
}
private static void printProducts(ArrayList<Pr
{
for (Product p : products)
{
printProduct(p);
}
System.out.println();
}
private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(
StringUtils.padWithSpaces(
p.getFormattedPrice();
System.out.println(product
}
}
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
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()
try this:
public class XMLTesterApp
{
private static String productsFilename = "products.xml";
public static void main(String[] args)
{
System.out.println("Produc ts 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("Produc ts 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("Produc ts 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.newInstanc e();
FileReader fileReader = new FileReader("products.xml") ;
XMLStreamReader reader = inputFactory.createXMLStre amReader(f ileReader) ;
while (reader.hasNext())
{
int eventType = reader.getEventType();
switch (eventType)
{
case XMLStreamConstants.START_E LEMENT:
String elementName = reader.getLocalName();
if (elementName.equals ("Product"))
{
p = new Product();
String code = reader.getAttributeValue(0 );
p.setCode(code);
}
if (elementName.equals("Descr iption"))
{
String description = reader.getElementText();
p.setDescription(descripti on);
}
if (elementName.equals("Price "))
{
String priceString = reader.getElementText();
double price = Double.parseDouble(priceSt ring);
p.setPrice(price);
}
break;
case XMLStreamConstants.END_ELE MENT:
elementName = reader.getLocalName();
if (elementName.equals("Produ ct"))
{
products.add(p);
}
break;
default:
break;
}
reader.next();
}
return products;
}
private static void writeProducts(ArrayList<Pr oduct> products)
{
// add code that writes the XML document to the products.xml file
}
private static void printProducts(ArrayList<Pr oduct> 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.getDescr iption(), 44) +
p.getFormattedPrice();
System.out.println(product String);
}
}
public class XMLTesterApp
{
private static String productsFilename = "products.xml";
public static void main(String[] args)
{
System.out.println("Produc
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("Produc
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("Produc
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.newInstanc
FileReader fileReader = new FileReader("products.xml")
XMLStreamReader reader = inputFactory.createXMLStre
while (reader.hasNext())
{
int eventType = reader.getEventType();
switch (eventType)
{
case XMLStreamConstants.START_E
String elementName = reader.getLocalName();
if (elementName.equals ("Product"))
{
p = new Product();
String code = reader.getAttributeValue(0
p.setCode(code);
}
if (elementName.equals("Descr
{
String description = reader.getElementText();
p.setDescription(descripti
}
if (elementName.equals("Price
{
String priceString = reader.getElementText();
double price = Double.parseDouble(priceSt
p.setPrice(price);
}
break;
case XMLStreamConstants.END_ELE
elementName = reader.getLocalName();
if (elementName.equals("Produ
{
products.add(p);
}
break;
default:
break;
}
reader.next();
}
return products;
}
private static void writeProducts(ArrayList<Pr
{
// add code that writes the XML document to the products.xml file
}
private static void printProducts(ArrayList<Pr
{
for (Product p : products)
{
printProduct(p);
}
System.out.println();
}
private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(
StringUtils.padWithSpaces(
p.getFormattedPrice();
System.out.println(product
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
ASKER
thank you