Solved

groovy/grails viewing xml example

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 55
jboss wildfly 10.1 10 241
Running JavaFX on JDeveloper 12C 1 55
How to convert from xls to xlsx using java 7 56
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 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