• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using MarkupBuilder

 
Greenhorn
Posts: 9
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to output this using MarkupBuilder

<?xml version="1.0" encoding="UTF-8"?>
<!-- comment here -->
<root>
<level1>
value
<level2/>
<level2/>
</level1>
<level1 name='name1'>
<level2 name='name2' />
</level1>
</root>
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello jhuniepi-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

Enjoy your time here.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Note: You generally would not explicitly set the encoding. StreamingMarkupBuilder will examine the encoding used by the Writer it's being written to and sets the encoding accordingly. If it can't determine the encoding it will use US-ASCII.

also the out << ans comment << notation has only recently been introduced for the 1.0 release. If you are using and older version then you need to do this:


This notation will work with both older versions of Groovy and 1.0 - older versions are not able to emit XML declarations, though
 
cj pangilinan
Greenhorn
Posts: 9
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tug Wilson:


Note: You generally would not explicitly set the encoding. StreamingMarkupBuilder will examine the encoding used by the Writer it's being written to and sets the encoding accordingly. If it can't determine the encoding it will use US-ASCII.

also the out << ans comment << notation has only recently been introduced for the 1.0 release. If you are using and older version then you need to do this:


This notation will work with both older versions of Groovy and 1.0 - older versions are not able to emit XML declarations, though




Here's the output:
<!--comment here--><root><level1>value<level2/><level2/></level1><level1 name='name1'><level2 name='name2'/></level1></root>

it's not in the form:

<!--comment here-->
<root>
<level1>
value
<level2/>
<level2/>
</level1>
<level1 name='name1'>
<level2 name='name2' />
</level1>
</root>


is there a way to use just groovy.xml.MarkupBuilder here?
just like in this syntax:

xml = new groovy.xml.MarkupBuilder()
xml.root {
level1("value"){
level2()
level2()
}
level1(name:"name1"){
level2(name:"name2")
}
}

PS:

i also changed my display name.. i'm sorry for not abiding the rule..
 
Tug Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MarkupBuilder does not generate XML declarations, comments or handle mixed content.

StreamingMarkupBuilder does not put any characters in the document unless you tell it to - hence it is not inserting newlines.

If you want break the lines then you can just add

lines.
 
cj pangilinan
Greenhorn
Posts: 9
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks tug. i made it.
StreamingMarkupBuilder is new and i can't find documentation for it yet..
 
Tug Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul King has done a great article on the Wiki which gives you a very good introduction to XML processing in Groovy http://docs.codehaus.org/display/GROOVY/Processing+XML
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic