Home > front end >  How to read and parse xml file with groovy
How to read and parse xml file with groovy

Time:11-30

May I know how to read and parse .xml file with groovy? The groovy file needs to read the xml and grab shop id and country information

<?xml version="1.0"?>
<Someinformation>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>
<Shops>
    <shop id ="shop1" index ="1">
    <ctr  country="Japan">01</ctr>
    <ctr  country="Korea">02</ctr>
    </shop>
    <shop id ="shop2" index ="2">
    <ctr  country="England">03</ctr>
    <ctr  country="Germany">04</ctr>
    </shop>
</Shops>
</Someinformation>

To open the .xml :

def xml=new XmlSlurper().parse("book.xml") 

But how to grab the xml contents?

CodePudding user response:

You can do something like that:

def xml = new  groovy.xml.XmlSlurper().parse("book.xml")

def total = xml.'*'.size()
println "Total amount of books: $total"
for (i in 0..<total) {
    def book = xml.book[i]
    println "-------------------------"
    println "ID: ${[email protected]()}"
    println "Author: ${book.author.text()}"
    println "Title: ${book.title.text()}"
    println "-------------------------"
}

Here I'm iterating over the list of books and print some of the data.

CodePudding user response:

It is best to study XPath and Gpath, described here.

But for your example, if we clean up the XML, consider this:

def someInfo = new XmlSlurper().parse("book.xml") 

someInfo.Shops.shop.each { thisShop ->
    // thisShop is the current node <shop> in XML
    println "shop id: "   thisShop."@id"
    thisShop.ctr.each { thisCtr ->
        // thisCtr is the current node <ctr> in XML
        println "country: "   thisCtr.country   " code: "   thisCtr.text()
    }
}

which gives this output:

shop id: shop1
country:  code: 01
country:  code: 02
shop id: shop2
country:  code: 03
country:  code: 04

Working example of the code is here.

CodePudding user response:

You can read the data you want without wasting resources for XML-parsing:

String xml = '''<?xml version="1.0"?> <Someinformation> <catalog>    <book id="bk101">       <author>Gambardella, Matthew</author>       <title>XML Developer's Guide</title>       <genre>Computer</genre>       <price>44.95</price>       <publish_date>2000-10-01</publish_date>       <description>An in-depth look at creating applications        with XML.</description>    </book>    <book id="bk102">       <author>Ralls, Kim</author>       <title>Midnight Rain</title>       <genre>Fantasy</genre>       <price>5.95</price>       <publish_date>2000-12-16</publish_date>       <description>A former architect battles corporate zombies,        an evil sorceress, and her own childhood to become queen        of the world.</description>    </book> </catalog> <Shops>     <shop id ="shop1" index ="1">     <ctr  country="Japan">01</ctr>     <ctr  country="Korea">02</ctr>     </shop>     <shop id ="shop2" index ="2">     <ctr  country="England">03</ctr>     <ctr  country="Germany">04</ctr>     </shop> </Shops> </Someinformation>'''

def shopIds = ( xml =~ /<shop id ="(\w )" index ="\d">/ ).findAll()*.last()
assert shopIds == ['shop1', 'shop2']

def countries = ( xml =~ /<ctr  country="(\w )">/ ).findAll()*.last()
assert countries == ['Japan', 'Korea', 'England', 'Germany']
  • Related