Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can one find the sum of node values stored in an XML file through java?
    text
    copied!<p>I apologize for the poor formatting and potentially stupid question...I am a noobie in these areas.</p> <p>I have a simple XML document that I have created to represent a hobby I enjoy, the items required for the hobby, and the price for those items. I am writing a Java application to read the XML file and output it's contents through using nodes. I have gotten that portion of the program to work correctly. </p> <p>However, what I am trying to figure out is, is it possible to take the values that represent the "price" values I am already pulling, and find the sum of them?</p> <p>I have read multiple different threads over the internet but none of them seem to fit my scenario.</p> <p>XML: </p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;Wakeboarding&gt; &lt;gear&gt; &lt;description&gt; Board &lt;/description&gt; &lt;price currency = "USD"&gt; 400.00 &lt;/price&gt; &lt;/gear&gt; &lt;gear&gt; &lt;description&gt; Bindings &lt;/description&gt; &lt;price currency = "USD"&gt; 200.00 &lt;/price&gt; &lt;/gear&gt; &lt;gear&gt; &lt;description&gt; Helmet &lt;/description&gt; &lt;price currency = "USD"&gt; 75.00 &lt;/price&gt; &lt;/gear&gt; &lt;gear&gt; &lt;description&gt; Lifevest &lt;/description&gt; &lt;price currency = "USD"&gt; 75.00 &lt;/price&gt; &lt;/gear&gt; &lt;gear&gt; &lt;description&gt; Handle &lt;/description&gt; &lt;price currency = "USD"&gt; 50.00 &lt;/price&gt; &lt;/gear&gt; &lt;/Wakeboarding&gt; </code></pre> <p>JAVA:</p> <pre><code>public class HobbyReader { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { try { File fXmlFile = new File("/Users/Pete/Documents/NetBeansProjects/HobbyXML/src/hobby.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("My Hobby: " + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("gear"); System.out.println("----------------------------"); for (int temp = 0; temp &lt; nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Gear Description: " + eElement.getElementsByTagName("description").item(0).getTextContent()); System.out.println("Price: " + eElement.getElementsByTagName("price").item(0).getTextContent()); } } </code></pre> <p>Output:</p> <pre><code>run: My Hobby: Wakeboarding ---------------------------- Gear Description: Board Price: 400.00 Gear Description: Bindings Price: 200.00 Gear Description: Helmet Price: 75.00 Gear Description: Lifevest Price: 75.00 Gear Description: Handle Price: 50.00 </code></pre> <p>Thank you in advance for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload