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:
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
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
Get step-by-step solutions from verified subject matter experts
