Question: I need to make this code read a .xml file. I created the .xml file in word and saved it as a .xml file. I

I need to make this code read a .xml file. I created the .xml file in word and saved it as a .xml file. I put it in the corresponding NetBeans Project File but it keeps showing errors when I run it. Please help me to get this working. ??? 

 

This is the code I have so far:

 

package lab.pkg9.salesperson.bonus;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

 


public class Lab9SalespersonBonus {

   /**
    * @param args the command line arguments
    * @throws java.io.IOException
    */
   
public static void main(String args[]) throws IOException
   {
       double sales;
       double totalSales = 0.0;
       double totalBonus = 0.0;
       double bonus;

try{
   //Open the file for reading Purpose
   FileReader fin = new FileReader("Salesperson.xml");

   //Scanner Class Instance
   Scanner Keyboard = new Scanner(fin);

   while (Keyboard.hasNext())
   {
       //Read the Sale from the File
       sales = Keyboard.nextDouble();
       totalSales = totalSales + sales;        //To get the total sales
       if(sales>=6000.00)
       {
           bonus = 0.05 * sales;           //to find bonus
           totalBonus = totalBonus + bonus;
       }
       
       else
       {
           bonus = 0.02 * sales;           //to find bonus
           totalBonus = totalBonus + bonus;
       }
   }
   //Close the file
   fin.close();
   //Print the total Sales and Bonus
   System.out.println("$" + String.format("%.2f",totalSales));
   System.out.println("$" + String.format("%.2f",totalBonus));
}

   //if the File is not found
   catch(FileNotFoundException fe)
   {
     System.out.println("FileNotFoundException : " + fe);
   }

   }
}

 

This is the .xml file content:

 




1
50000


2
60000


3
70000


4
80000


5
90000


6
100000


7
110000


8
120000


9
130000


10
140000

 

I know I am supposed to parse this but I can not figure out what do do to get it to work. Not sure how to parse.

 

Please help ??

  

Step by Step Solution

3.39 Rating (177 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To parse an XML file in Java you typically use libraries like DOM Document Object Model or SAX Simple API for XML Since your XML file seems relatively ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!