I have an assignment which is two parts. The first part I have solved. It calls for me to write a program that can read an XML file which then creates a purse a purse object and then prints the total value of the coins in the purse. The second part(PurseXMLDemo) then counts up the coins and produces a new XML file as output. This is the part I can't figure out.

Here are all the classes and files for the solution

/**
   A coin with a monetary value.
*/
public class Coin
{
   /**
      Constructs a coin.
      @param aValue the monetary value of the coin.
      @param aName the name of the coin
   */
   public Coin(double aValue, String aName) 
   { 
      value = aValue; 
      name = aName;
   }

   /**
      Gets the coin value.
      @return the value
   */
   public double getValue() 
   {
      return value;
   }

   /**
      Gets the coin name.
      @return the name
   */
   public String getName() 
   {
      return name;
   }

   private double value;
   private String name;
}

/**
   A purse computes the total value of a collection of coins.
*/
public class Purse
{
   /**
      Constructs an empty purse.
   */
   public Purse()
   {
      total = 0;
   }

   /**
      Add a coin to the purse.
      @param aCoin the coin to add
   */
   public void add(Coin aCoin)
   {
      total = total + aCoin.getValue();
   }

   /**
      Get the total value of the coins in the purse.
      @return the sum of all coin values
   */
   public double getTotal()
   {
      return total;
   }

   private double total;
}

import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

/**
   Builds a DOM document for a Purse with coins.
*/
public class PurseBuilder
{
   private DocumentBuilder builder;
   private Document doc;
   private XPath path;

   /**
      Constructs a Purse builder.
   */
   public PurseBuilder() 
         throws ParserConfigurationException
   {
      DocumentBuilderFactory factory 
            = DocumentBuilderFactory.newInstance();
      builder = factory.newDocumentBuilder();
   }

   /**
      Builds a DOM document for a Purse with coins.
      @param c the coins
      @return a DOM document describing the coins
   */
   public Document build(Purse c)throws SAXException, IOException, XPathExpressionException
   {
      doc = builder.newDocument();
      doc.appendChild(createItems(c));
      return doc;
   }

   /**
      Builds a DOM element for a Purse with coins.
      @param coins
      @return a DOM element describing the coins
   */      
   private Element createItems(Purse c) throws SAXException, IOException, XPathExpressionException
   {      
      Element e = doc.createElement("Purse");

      XPathFactory xpfactory = XPathFactory.newInstance();
      path = xpfactory.newXPath();

      Purse p = new Purse();
      int itemCount = Integer.parseInt(path.evaluate(
            "count(/purse/coin)", doc));
      for (int i = 1; i <= itemCount; i++)
      {
         String coinN = path.evaluate(
               "/purse/coin[" + i + "]/value/name", doc);
         double price = Double.parseDouble(path.evaluate(
               "/purse/coin[" + i + "]/value", doc));
         Coin coin = new Coin(price, coinN);
         p.add(coin);
      }
      return e;
   }

   /**
      Builds a DOM element for an item.
      @param anItem the item
      @return a DOM element describing the item
   */
   private Element createItem(Coin anItem)
   {      
      Element e = doc.createElement("coins");

      e.appendChild(createCoin(anItem));
      e.appendChild(createTextElement(
            "quantity", "" + anItem.getValue()));

      return e;
   }

   /**
      Builds a DOM element for a product.
      @param p the product
      @return a DOM element describing the product
   */
   private Element createCoin(Coin c)
   {
      Element e = doc.createElement("coin");

      e.appendChild(createTextElement(
            "Value",""+ c.getValue()));
      e.appendChild(createTextElement(
            "Name", "" + c.getName()));

      return e;
   }

   private Element createTextElement(String name, String text)
   {
      Text t = doc.createTextNode(text);
      Element e = doc.createElement(name);
      e.appendChild(t);
      return e;
   }
}

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
   An XML parser for a purse
*/
public class PurseParser
{
   private DocumentBuilder builder;
   private XPath path;

   /**
      Constructs a parser that can parse item lists.
   */
   public PurseParser() 
         throws ParserConfigurationException
   {
      DocumentBuilderFactory dbfactory 
            = DocumentBuilderFactory.newInstance();
      builder = dbfactory.newDocumentBuilder();
      XPathFactory xpfactory = XPathFactory.newInstance();
      path = xpfactory.newXPath();
   }

   /**
      Parses an XML file containing a purse.
      @param fileName the name of the file
      @return an array list containing all items in the XML file
   */
   public Purse parse(String fileName) 
      throws SAXException, IOException, XPathExpressionException
   {
      File f = new File(fileName);
      Document doc = builder.parse(f);

      Purse p = new Purse();
      int itemCount = Integer.parseInt(path.evaluate(
            "count(/purse/coin)", doc));
      for (int i = 1; i <= itemCount; i++)
      {
         String coinN = path.evaluate(
               "/purse/coin[" + i + "]/value/name", doc);
         double price = Double.parseDouble(path.evaluate(
               "/purse/coin[" + i + "]/value", doc));
         Coin c = new Coin(price, coinN);
         p.add(c);
      }
      return p;
   }
}


/**
   This program parses an XML file containing an coin list.
   It prints out the total value of coins in the purse that
   are described in the XML file.
*/
public class PurseParserDemo
{
   public static void main(String[] args) throws Exception
   {
      PurseParser parser = new PurseParser();
      Purse p = parser.parse(args[0]);
      System.out.println("Total coin value: " + p.getTotal());
   }
}

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;

/**
   This program parses an XML file containing an coin list.
   It prints out the quantity of coin types in the purse that
   are described in the XML file.
*/
public class PurseXMLDemo
{
   public static void main(String[] args) throws Exception
   {
      PurseParser parser = new PurseParser();
      Purse p = parser.parse(args[0]);

      PurseBuilder builder = new PurseBuilder();
      Document doc = builder.build(p);
      DOMImplementation impl = doc.getImplementation();
      DOMImplementationLS implLS
           = (DOMImplementationLS) impl.getFeature("LS", "3.0");
      LSSerializer ser = implLS.createLSSerializer();
      ser.getDomConfig().setParameter("format-pretty-print", true);
      String out = ser.writeToString(doc);
      System.out.println(out);
   }
}

Like I said, the PurseParserDemo works fine. It is the PurseXMLDemo which does not work correctly. It compiles but then it will only produce the first line of the header and that's it. Any help would be appreciated.

Still need some help for this one, due at midnight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.