xp1632 / DFKI_working_log

0 stars 0 forks source link

XML in Groovy #26

Open xp1632 opened 1 year ago

xp1632 commented 1 year ago

https://www.tutorialspoint.com/groovy/groovy_xml.htm


we can also parse and display XML document by xmlParser:

import groovy.xml.MarkupBuilder 
import groovy.util.*

class Example {

   static void main(String[] args) { 

      def parser = new XmlParser()
      def doc = parser.parse("D:\\Movies.xml");

      doc.movie.each{
         bk->
         print("Movie Name:")
         println "${bk['@title']}"

         print("Movie Type:")
         println "${bk.type[0].text()}"

         print("Movie Format:")
         println "${bk.format[0].text()}"

         print("Movie year:")
         println "${bk.year[0].text()}"

         print("Movie rating:")
         println "${bk.rating[0].text()}"

         print("Movie stars:")
         println "${bk.stars[0].text()}"

         print("Movie description:")
         println "${bk.description[0].text()}"
         println("*******************************")
      }
   }
} 

we get :

Movie Name:Enemy Behind 
Movie Type:War, Thriller 
Movie Format:DVD 
Movie year:2003 
Movie rating:PG 
Movie stars:10 
Movie description:Talk about a US-Japan war 
******************************* 
Movie Name:Transformers 
Movie Type:Anime, Science Fiction 
Movie Format:DVD 
Movie year:1989 
Movie rating:R 
Movie stars:8 
Movie description:A schientific fiction 
******************************* 
Movie Name:Trigun 
Movie Type:Anime, Action
Movie Format:DVD 
Movie year:1986 
Movie rating:PG 
Movie stars:10 
Movie description:Vash the Stam pede! 
******************************* 
Movie Name:Ishtar 
Movie Type:Comedy 
Movie Format:VHS 
Movie year:1987 
Movie rating:PG 
Movie stars:2 
Movie description:Viewable boredom
xp1632 commented 1 year ago

Json :https://www.tutorialspoint.com/groovy/groovy_json.htm