File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Languages and the fly likes Using MarkupBuilder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Using MarkupBuilder" Watch "Using MarkupBuilder" New topic
Author

Using MarkupBuilder

cj pangilinan
Greenhorn

Joined: Dec 22, 2006
Posts: 9
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
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Tug Wilson
Ranch Hand

Joined: Dec 12, 2006
Posts: 33


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

Joined: Dec 22, 2006
Posts: 9
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

Joined: Dec 12, 2006
Posts: 33
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

Joined: Dec 22, 2006
Posts: 9
thanks tug. i made it.
StreamingMarkupBuilder is new and i can't find documentation for it yet..
Tug Wilson
Ranch Hand

Joined: Dec 12, 2006
Posts: 33
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using MarkupBuilder
 
Similar Threads
Hashtable doubt
call-template name selection - need help...
JQuery with xml Div
package problem
Constructing an XML file