Question

Write a program that can read an XML file based on a hobby of yours. Your...

Write a program that can read an XML file based on a hobby of yours. Your XML file should contain at least 5 items and that one of the details tracked must be "cost". Create a program that reads through the items in your XML file and adds the total value of the cost of each item. Display the total cost to the user. (No other output is required.) Your project should be named LastnameFirstnameHW7. Before Zipping your project please place the XML data file in the top-level folder of your Netbeans project.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have solved the required problem please place the both file in NetBeans.

The Below XML created for solution save as hobbies.xml

<?xml version="1.0" encoding="UTF-8"?>

<Hobbies>

<Hobby>

<name>Singing</name>

<cost>245</cost>

</Hobby>

<Hobby>

<name>Watching Movie</name>

<cost>452</cost>

</Hobby>

<Hobby>

<name>Playing Game</name>

<cost>300</cost>

</Hobby>

<Hobby>

<name>Coding</name>

<cost>200</cost>

</Hobby>

<Hobby>

<name>Football</name>

<cost>400</cost>

</Hobby>

</Hobbies>

below java file created for parsing the xml save below file as XMLParserHW7.java or any other name if u want but make sure the file should have extension of .java

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

/**

*This class will used to parse xml and sum the total cost

*/

public class XMLParserHW7 {

public static void main(String[] args) {

//taken cost variable for storing cost

double cost=0;

//name of xml file

String fileName = "hobbies.xml";

File xmlFile = new File(fileName);

//using java library

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

try{

builder = dbf.newDocumentBuilder();

//parsing xml file in Document instance

Document doc = builder.parse(xmlFile);

doc.getDocumentElement().normalize();

//root name of XML as per given in file

NodeList nodeL = doc.getElementsByTagName("Hobby");

//looping through the nodes of XML file

for(int i=0;i<nodeL.getLength();i++)

{

Node node = nodeL.item(i);

if(node.getNodeType()==Node.ELEMENT_NODE)

{

Element ele = (Element) node;

//parsing the String value to double

//and adding to cost

//getting the value from XML file with have tag name

//have cost as similar way can get value of any tag name

//please cast as per your requirement

cost+=Double.parseDouble(ele

.getElementsByTagName("cost")

.item(0)

.getTextContent()) ;

}

}

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

//printing the total cost

System.out.println("Total Cost = "+cost);

}

}

OUTPUT:

<terminated> XMLParserHW7 [Java Application] C:AProgram Files Javajre1.8.013 otal Cost = 1597.0

please do let me know if u have any doubts... i will be replying ASAP...

Add a comment
Know the answer?
Add Answer to:
Write a program that can read an XML file based on a hobby of yours. Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this project your task is to update the RSS Reader program you wrote for the...

    For this project your task is to update the RSS Reader program you wrote for the previous project so that it reads multiple RSS feeds and generates the same nicely formatted HTML page of links for each feed, plus an HTML index page with links to the individual feed pages. Your new program should ask the user for the name of an XML file containing a list of URLs for RSS v2.0 feeds (see below for the format of this...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • Shopping Cart You are to write a program that reads the name of the item, the quantity of each it...

    C++ language Shopping Cart You are to write a program that reads the name of the item, the quantity of each item, and the cost per item from a file. The program will then print out the item and how much is owed for each item. After everything is read in, it will print out the total cost of the cart. Your program should have the following functions main - This function asks the user to enter a filename and...

  • Write a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".

    Project DescriptionWrite a program that calculates the average number of days a company's employees are absent during the year and outputs a report on a file named "employeeAbsences.txt".Project SpecificationsInput for this project:the user must enter the number of employees in the company.the user must enter as integers for each employee:the employee number (ID)the number of days that employee missed during the past year.Input Validation:Do not accept a number less than 1 for the number of employees.Do not accept a negative...

  • Write a program that can read XML files for customer accounts receivable, such as <ar>        <customerAccounts>...

    Write a program that can read XML files for customer accounts receivable, such as <ar>        <customerAccounts>               <customerAccount>                       <name>Apple County Grocery</name>                       <accountNumber>1001</accountNumber>                       <balance>1565.99</balance>               </customerAccount>               <customerAccount>                       <name>Uptown Grill</name>                       <accountNumber>1002</accountNumber>                       <balance>875.20</balance>               </customerAccount>        </customerAccounts>        <transactions>               <payment>                       <accountNumber>1002</accountNumber>                       <amount>875.20</amount>               </payment>               <purchase>                       <accountNumber>1002</accountNumber>                       <amount>400.00</amount>               </purchase>               <purchase>                       <accountNumber>1001</accountNumber>                       <amount>99.99</amount>               </purchase>               <payment>                       <accountNumber>1001</accountNumber>                       <amount>1465.98</amount>               </payment>        </transactions>     </ar> Your program should construct an CustomerAccountsParser object, parse the XML data & create new CustomerAccount objects in the CustomerAccountsParser for each of the products the XML data, execute all of...

  • Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a...

    Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a file called Numbers.txt in your the same folder as your Python program. This file should contain a list on floating point numbers, one on each line. The number should be greater than zero but less than one million. Your program should read the following and display: The number of numbers in the file (i.e. count them) (counter) The maximum number in the file (maximum)...

  • in c++ please. Write a program that reads the contents of a text file. The program...

    in c++ please. Write a program that reads the contents of a text file. The program should create a map in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the map would contain an element with "the" as the key and 128 as the value. The program should either display the frequency of each word or create...

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT