Skip to content

Instantly share code, notes, and snippets.

@fganora
Created October 21, 2016 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fganora/5edf9aea69e18ba38e3a33c53be6ef55 to your computer and use it in GitHub Desktop.
Save fganora/5edf9aea69e18ba38e3a33c53be6ef55 to your computer and use it in GitHub Desktop.
Groovy: simple example of parsing and processing an XML document with XmlSlurper
def menu = new XmlSlurper().parseText(payload)
def totalPrice = 0.0
(menu.food).each {
menuItem ->
totalPrice += Double.parseDouble(menuItem.price.text())
}
totalPrice
@fganora
Copy link
Author

fganora commented Oct 21, 2016

Sample input XML for this:

<?xml version="1.0" encoding="UTF-8"?>
<menu>
    <food>
        <name>Belgian Waffles</name>
        <price>5.95</price>
        <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>7.95</price>
        <description>Light Belgian waffles covered with strawberries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <name>Berry-Berry Belgian Waffles</name>
        <price>8.95</price>
        <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
        <calories>900</calories>
    </food>
    <food>
        <name>French Toast</name>
        <price>4.50</price>
        <description>Thick slices made from our homemade sourdough bread</description>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>6.95</price>
        <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
        <calories>950</calories>
    </food>
</menu>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment