• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using MarkupBuilder

 
cj pangilinan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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>
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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.
 
Tug Wilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic