Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

groovy/grails viewing xml example

Posted on 2011-03-11
2
Medium Priority
?
485 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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

688 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