Solved

groovy/grails viewing xml example

Posted on 2011-03-11
2
476 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error with Java/Cache JDBC Classpath 2 31
JUnit 4 @Before and @BeforeClass differences 3 48
Configure a Bean in an XML file 4 14
servlet example 17 10
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now