Translate

Wednesday 6 October 2021

Working with Builders MarkupBuilder Exercise in Groovy Scripting groovy training telugu 62

 Watch This Video


https://youtu.be/X3UkB5ONEAU

-------------------------------------------------------------------------------------------------------

[Exercise] MarkupBuilder
XML
Use the MarkupBuilder to generate the following XML

<books>
    <book isbn="978-1935182443">
        <title>Groovy in Action 2nd Edition</title>
        <author>Dierk Koenig</author>
        <price>50.58</price>
    </book>
    <book isbn="978-1935182948">
        <title>Making Java Groovy</title>
        <author>Ken Kousen</author>
        <price>33.96</price>
    </book>
    <book isbn="978-1937785307">
        <title>Programming Groovy 2: Dynamic Productivity for the Java Developer</title>
        <author>Venkat Subramaniam</author>
        <price>28.92</price>
    </book>
</books>


HTML
With the same data from the xml version build an HTML page that lists that data.

Bonus
Using a FileWriter write the contents of the HTML from the MarkupBuilder to a file.



//CODE

//html.groovy

import groovy.xml.MarkupBuilder


def books =[
        [isbn:"978-1935182443",title:"Groovy in Action 2nd Edition",author:"Dierk Koeting",priece:50.58],
        [isbn:"978-1935182948",title:"Making Java Groovy",author:"Ken Kousen",priece:33.96],
        [isbn:"978-1937785307",title:"Programming Groovy 2:Dynamic Productivity for the Java Developer",author:"Venkat Subramaniam",priece:28.92]
]

MarkupBuilder builder = new MarkupBuilder()

builder.html{
    head{
        title("My Favorite Books")

    }
    body{
        h1("My Favorite Books")
        table{
            tr{
                th("ISBN")
                th("Title")
                th("Author")
                th("Price")

            }
            books.each {book ->
                tr{
                    td(book.isbon)
                    td(book.title)
                    td(book.author)
                    td(book.price)


                }

            }
        }


    }
}





//xml.groovy


import groovy.xml.MarkupBuilder

MarkupBuilder builder=new MarkupBuilder()
builder.books{
    book(isbn: "978-1935182443"){
        title("Groovy in Action 2nd Edition")
        author("Dierk Koeting")
        price("50.58")
    }
    book(isbn: "978-1935182948"){
        title("Making Java Groovy")
        author("Ken Kousen")
        price("33.96")
    }
    book(isbn: "978-1937785307"){
        title("Programming Groovy 2:Dynamic Productivity for the Java Developer")
        author("Venkat Subramaniam")
        price("28.92")
    }
}
























No comments:

Post a Comment

Note: only a member of this blog may post a comment.