Groovy web console

subscribe to the feed Subscribe
to this
site
extract value from xml (via #groovywebconsole)
tweet this snippet Tweet
this
script

extract value from xml

Published 4 months ago by nmrao with tags parse
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xml = """<Response xmlns="https://www.googleapis.com/books/v1/volumes">
   <items>
      <e>
         <accessInfo>
            <accessViewStatus>NONE</accessViewStatus>
            <country>IN</country>
            <embeddable>false</embeddable>
            <epub>
               <isAvailable>false</isAvailable>
            </epub>
            <pdf>
               <isAvailable>false</isAvailable>
            </pdf>
            <publicDomain>false</publicDomain>
            <quoteSharingAllowed>false</quoteSharingAllowed>
            <textToSpeechPermission>ALLOWED</textToSpeechPermission>
            <viewability>NO_PAGES</viewability>
            <webReaderLink>http://books.google.co.in/books/reader?id=8bkxV-WcLFoC&hl=&printsec=frontcover&output=reader&source=gbs_api</webReaderLink>
         </accessInfo>
         <etag>iKsLXIuXjx8</etag>
         <id>8bkxV-WcLFoC</id>
         <kind>books#volume</kind>
         <saleInfo>
            <country>IN</country>
            <isEbook>false</isEbook>
            <saleability>NOT_FOR_SALE</saleability>
         </saleInfo>
         <searchInfo>
            <textSnippet>Beginning with an &quot;Ant on an Apple&quot;, illustrations, simple text, and a pesky fly who will not stay on his page introduce the letters of the alphabet.</textSnippet>
         </searchInfo>
         <selfLink>https://www.googleapis.com/books/v1/volumes/8bkxV-WcLFoC</selfLink>
         <volumeInfo>
            <allowAnonLogging>false</allowAnonLogging>
            <authors>
               <e>Bruno Munari</e>
            </authors>
            <canonicalVolumeLink>https://books.google.com/books/about/ABC.html?hl=&id=8bkxV-WcLFoC</canonicalVolumeLink>
            <contentVersion>0.0.1.0.preview.0</contentVersion>
            <imageLinks>
               <smallThumbnail>http://books.google.com/books/content?id=8bkxV-WcLFoC&printsec=frontcover&img=1&zoom=5&source=gbs_api</smallThumbnail>
               <thumbnail>http://books.google.com/books/content?id=8bkxV-WcLFoC&printsec=frontcover&img=1&zoom=1&source=gbs_api</thumbnail>
            </imageLinks>
            <industryIdentifiers>
               <e>
                  <identifier>MINN:319510009151144</identifier>
                  <type>OTHER</type>
               </e>
            </industryIdentifiers>
            <infoLink>http://books.google.co.in/books?id=8bkxV-WcLFoC&dq=abc&hl=&source=gbs_api</infoLink>
            <language>en</language>
            <maturityRating>NOT_MATURE</maturityRating>
            <panelizationSummary>
               <containsEpubBubbles>false</containsEpubBubbles>
               <containsImageBubbles>false</containsImageBubbles>
            </panelizationSummary>
            <previewLink>http://books.google.co.in/books?id=8bkxV-WcLFoC&q=abc&dq=abc&hl=&cd=1&source=gbs_api</previewLink>
            <printType>BOOK</printType>
            <publishedDate>1960</publishedDate>
            <readingModes>
               <image>false</image>
               <text>false</text>
            </readingModes>
            <title>ABC.</title>
         </volumeInfo>
      </e>
      <e>
         <accessInfo>
            <accessViewStatus>NONE</accessViewStatus>
            <country>IN</country>
            <embeddable>false</embeddable>
            <epub>
               <isAvailable>false</isAvailable>
            </epub>
            <pdf>
               <isAvailable>false</isAvailable>
            </pdf>
            <publicDomain>false</publicDomain>
            <quoteSharingAllowed>false</quoteSharingAllowed>
            <textToSpeechPermission>ALLOWED</textToSpeechPermission>
            <viewability>NO_PAGES</viewability>
            <webReaderLink>http://books.google.co.in/books/reader?id=7s4WAQAAMAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api</webReaderLink>
         </accessInfo>
         <etag>miIPSeJ57M8</etag>
         <id>7s4WAQAAMAAJ</id>
         <kind>books#volume</kind>
         <saleInfo>
            <country>IN</country>
            <isEbook>false</isEbook>
            <saleability>NOT_FOR_SALE</saleability>
         </saleInfo>
         <selfLink>https://www.googleapis.com/books/v1/volumes/7s4WAQAAMAAJ</selfLink>
         <volumeInfo>
            <allowAnonLogging>false</allowAnonLogging>
            <authors>
               <e>Bruno Munari</e>
            </authors>
            <canonicalVolumeLink>https://books.google.com/books/about/ABC.html?hl=&id=7s4WAQAAMAAJ</canonicalVolumeLink>
            <categories>
               <e>Juvenile Nonfiction</e>
            </categories>
            <contentVersion>preview-1.0.0</contentVersion>
            <imageLinks>
               <smallThumbnail>http://books.google.com/books/content?id=7s4WAQAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api</smallThumbnail>
               <thumbnail>http://books.google.com/books/content?id=7s4WAQAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api</thumbnail>
            </imageLinks>
            <industryIdentifiers>
               <e>
                  <identifier>UOM:49015000911801</identifier>
                  <type>OTHER</type>
               </e>
            </industryIdentifiers>
            <infoLink>http://books.google.co.in/books?id=7s4WAQAAMAAJ&dq=abc&hl=&source=gbs_api</infoLink>
            <language>en</language>
            <maturityRating>NOT_MATURE</maturityRating>
            <previewLink>http://books.google.co.in/books?id=7s4WAQAAMAAJ&q=abc&dq=abc&hl=&cd=2&source=gbs_api</previewLink>
            <printType>BOOK</printType>
            <publishedDate>1960</publishedDate>
            <readingModes>
               <image>false</image>
               <text>false</text>
            </readingModes>
            <title>ABC.</title>
         </volumeInfo>
      </e>
      <e>
         <accessInfo>
            <accessViewStatus>NONE</accessViewStatus>
            <country>IN</country>
            <embeddable>false</embeddable>
            <epub>
               <isAvailable>false</isAvailable>
            </epub>
            <pdf>
               <isAvailable>false</isAvailable>
            </pdf>
            <publicDomain>false</publicDomain>
            <quoteSharingAllowed>false</quoteSharingAllowed>
            <textToSpeechPermission>ALLOWED</textToSpeechPermission>
            <viewability>NO_PAGES</viewability>
            <webReaderLink>http://books.google.co.in/books/reader?id=Q6LsAAAAMAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api</webReaderLink>
         </accessInfo>
         <etag>wHyvfXqhLU8</etag>
         <id>Q6LsAAAAMAAJ</id>
         <kind>books#volume</kind>
         <saleInfo>
            <country>IN</country>
            <isEbook>false</isEbook>
            <saleability>NOT_FOR_SALE</saleability>
         </saleInfo>
         <searchInfo>
            <textSnippet>An intense examination of the effects of technology on literacy and language.</textSnippet>
         </searchInfo>
         <selfLink>https://www.googleapis.com/books/v1/volumes/Q6LsAAAAMAAJ</selfLink>
         <volumeInfo>
            <allowAnonLogging>false</allowAnonLogging>
            <authors>
               <e>Ivan Illich</e>
               <e>Barry Sanders</e>
            </authors>
            <canonicalVolumeLink>https://books.google.com/books/about/ABC.html?hl=&id=Q6LsAAAAMAAJ</canonicalVolumeLink>
            <categories>
               <e>Information storage and retrieval systems</e>
            </categories>
            <contentVersion>1.1.1.0.preview.0</contentVersion>
            <description>An intense examination of the effects of technology on literacy and language. The authors argue that there is a phenomenon transforming modern culture--language is becoming part of a technology of "information systems" with an emphasis on control, rather than human exchange. As a result, all language is becoming debased.</description>
            <imageLinks>
               <smallThumbnail>http://books.google.com/books/content?id=Q6LsAAAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api</smallThumbnail>
               <thumbnail>http://books.google.com/books/content?id=Q6LsAAAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api</thumbnail>
            </imageLinks>
            <industryIdentifiers>
               <e>
                  <identifier>IND:30000036960999</identifier>
                  <type>OTHER</type>
               </e>
            </industryIdentifiers>
            <infoLink>http://books.google.co.in/books?id=Q6LsAAAAMAAJ&dq=abc&hl=&source=gbs_api</infoLink>
            <language>en</language>
            <maturityRating>NOT_MATURE</maturityRating>
            <pageCount>166</pageCount>
            <previewLink>http://books.google.co.in/books?id=Q6LsAAAAMAAJ&q=abc&dq=abc&hl=&cd=3&source=gbs_api</previewLink>
            <printType>BOOK</printType>
            <publishedDate>1988</publishedDate>
            <publisher>Marion Boyars Publishers</publisher>
            <readingModes>
               <image>false</image>
               <text>false</text>
            </readingModes>
            <subtitle>The Alphabetization of the Popular Mind</subtitle>
            <title>ABC</title>
         </volumeInfo>
      </e>
      <e>
         <accessInfo>
            <accessViewStatus>SAMPLE</accessViewStatus>
            <country>IN</country>
            <embeddable>true</embeddable>
            <epub>
               <isAvailable>false</isAvailable>
            </epub>
            <pdf>
               <isAvailable>false</isAvailable>
            </pdf>
            <publicDomain>false</publicDomain>
            <quoteSharingAllowed>false</quoteSharingAllowed>
            <textToSpeechPermission>ALLOWED</textToSpeechPermission>
            <viewability>PARTIAL</viewability>
            <webReaderLink>http://books.google.co.in/books/reader?id=f1vdiFRI52gC&hl=&printsec=frontcover&output=reader&source=gbs_api</webReaderLink>
         </accessInfo>
         <etag>R8E127WkV8s</etag>
         <id>f1vdiFRI52gC</id>
         <kind>books#volume</kind>
         <saleInfo>
            <country>IN</country>
            <isEbook>false</isEbook>
            <saleability>NOT_FOR_SALE</saleability>
         </saleInfo>
         <searchInfo>
            <textSnippet>Includes 26 scenes which depict a Letterland character surrounded by objects beginning with the target sound. This children&#39;s alphabet book helps children in promoting phonemic awareness and vocabulary development.</textSnippet>
         </searchInfo>
         <selfLink>https://www.googleapis.com/books/v1/volumes/f1vdiFRI52gC</selfLink>
         <volumeInfo>
            <allowAnonLogging>false</allowAnonLogging>
            <authors>
               <e>Lyn Wendon</e>
            </authors>
            <canonicalVolumeLink>https://books.google.com/books/about/ABC.html?hl=&id=f1vdiFRI52gC</canonicalVolumeLink>
            <categories>
               <e>Alphabet</e>
            </categories>
            <contentVersion>0.2.0.0.preview.1</contentVersion>
            <description>Includes 26 scenes which depict a Letterland character surrounded by objects beginning with the target sound. This children's alphabet book helps children in promoting phonemic awareness and vocabulary development.</description>
            <imageLinks>
               <smallThumbnail>http://books.google.com/books/content?id=f1vdiFRI52gC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api</smallThumbnail>
               <thumbnail>http://books.google.com/books/content?id=f1vdiFRI52gC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api</thumbnail>
            </imageLinks>
            <industryIdentifiers>
               <e>
                  <identifier>9781862092211</identifier>
                  <type>ISBN_13</type>
               </e>
               <e>
                  <identifier>1862092214</identifier>
                  <type>ISBN_10</type>
               </e>
            </industryIdentifiers>
            <infoLink>http://books.google.co.in/books?id=f1vdiFRI52gC&dq=abc&hl=&source=gbs_api</infoLink>
            <language>en</language>
            <maturityRating>NOT_MATURE</maturityRating>
            <pageCount>58</pageCount>
            <previewLink>http://books.google.co.in/books?id=f1vdiFRI52gC&printsec=frontcover&dq=abc&hl=&cd=4&source=gbs_api</previewLink>
            <printType>BOOK</printType>
            <publishedDate>2003-04-01</publishedDate>
            <publisher>Letterland</publisher>
            <readingModes>
               <image>true</image>
               <text>false</text>
            </readingModes>
            <title>ABC</title>
         </volumeInfo>
      </e>

       </items>
       <kind>books#volumes</kind>
       <totalItems>2279</totalItems>
    </Response>"""
def response = new XmlSlurper().parseText(xml)
//Below would display all country names from multiple `e` tags
println "All country names : ${response.items.e.saleInfo.country}"
    
//Below would display country name from first `e` tag, increase the index to get the appropriate one in place of 0
println "Country name from first e : ${response.items.e[0].saleInfo.country}"
    
//Below to show the conditional country
//Get the country name if id is 8bkxV-WcLFoC
print 'conditional fetch of country name where id is 8bkxV-WcLFoC :'
print response.items.e.'*'.find{ it.name() == 'id' && it == '8bkxV-WcLFoC' }.parent().saleInfo.country
println ''
//Below to show the conditional country
//Get the country name if id is 8bkxV-WcLFoC
print 'conditional fetch of country name where type is ISBN_10 :'
print response.items.e.'**'.find{ it.name() == 'type' && it == 'ISBN_10' }.parent().parent().parent().parent().saleInfo.country