Solved

groovy/grails viewing xml example

Posted on 2011-03-11
2
481 Views
Last Modified: 2013-11-13
From this code example from the book "Grails in Action", how would i print the actual formatted XML?  Do I use file.txt?
import groovy.xml.MarkupBuilder
import groovy.util.XmlSlurper

def file = new File ("test.xml")
def objs = [
[quantity:10, name:"Orange",type:"Fruit"],
[quantity:6, name:"Apple",type:"Fruit"],
[quantity:2, name:"Chair",type:"Furniture"]]
def b = new MarkupBuilder(new FileWriter(file))
b.root{
  objs.each{o ->
    item(qty:o.quantity) {
      name(o.name)
      type(o.type)
      }
      }
      }
 def xml = new XmlSlurper().parse(file)
 assert xml.item.size()==3
 assert xml.item[0].name =="Orange"
 assert xml.item[0].@qty == "10"
 println "Fruits:${xml.item.findAll{it.type=='Fruit'}*.name}"
 println "Total:${xml.item.@qty.list().sum{it.toInteger()}}"

Open in new window

0
Comment
Question by:Melodi Roberts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
wannabetechie earned 250 total points
ID: 35512048
You can print the xml from the XmlSlurper like this

println XmlUtil.serialize(xml)

Open in new window


If you want to print directly from the MarkupBuilder then you need to pass StringWriter to the constructor.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb mdb examples 1 39
Java syntax, or is it Selenium 6 62
batch vs regular insert in spring DAO 2 52
listing all the respondents to a twitter feed - Java 5 48
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question