Quantcast

groovy.xml.MarkupBuilder

classic Classic list List threaded Threaded
17 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

groovy.xml.MarkupBuilder

Russel Winder
I have almost certainly just missed something obvious but is there any
easy idiomatic way of getting <?xml...?> and <!DOCTYPE...> tags out of
the groovy.xml.MarkupBuilder?  JDOM and dom4j make this quite easy.

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

tugwilson

On 20 Mar 2006, at 21:03, Russel Winder wrote:

> I have almost certainly just missed something obvious but is there any
> easy idiomatic way of getting <?xml...?> and <!DOCTYPE...> tags out of
> the groovy.xml.MarkupBuilder?  JDOM and dom4j make this quite easy.

No but you can do it with StreamingMarkupBuilder.

ther's a command called mkp.yieldUnescaped which lets you drop  
arbitrary CDATA into XML documents so you can use that for the XML  
declaration and the internal DTD subset.

It lets lets you insert PIs and comments

mkp.comment "this is a comment"

mkp.pi("myTarget", "myInstruction")

mkp.pi("myTarget", [name1: "value1", name2: "value2"])


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

Russel Winder
On Mon, 2006-03-20 at 21:27 +0000, John Wilson wrote:

> > I have almost certainly just missed something obvious but is there any
> > easy idiomatic way of getting <?xml...?> and <!DOCTYPE...> tags out of
> > the groovy.xml.MarkupBuilder?  JDOM and dom4j make this quite easy.
>
> No but you can do it with StreamingMarkupBuilder.

That seems a shame.  I tried replacing MarkupBuilder with Streaming
MarkupBuilder in a script that worked but now it produces:

Caught: groovy.lang.MissingMethodException: No signature of method
groovy.xml.StreamingMarkupBuilder.contacts() is applicable for argument
types: (writeContacts_better$_run_closure1) values:
{writeContacts_better$_run_closure1@c931fc}
        at
groovy.xml.streamingmarkupsupport.AbstractStreamingBuilder.invokeMethod(AbstractStreamingBuilder.groovy)
        at writeContacts_better.run(writeContacts_better.groovy:6)
        at writeContacts_better.main(writeContacts_better.groovy)

Which means that StreamingMarkupBuilder must be used very differently to
MarkupBuilder.  Google doesn't have any  StreamingMarkupBuilder examples
indexed from what I can see quickly.

> ther's a command called mkp.yieldUnescaped which lets you drop  
> arbitrary CDATA into XML documents so you can use that for the XML  
> declaration and the internal DTD subset.
>
> It lets lets you insert PIs and comments
>
> mkp.comment "this is a comment"
>
> mkp.pi("myTarget", "myInstruction")
>
> mkp.pi("myTarget", [name1: "value1", name2: "value2"])
This sort of code doesn't work for me :-(

As a side question:  I assume there is a parameter you can give in the
constructor to the MarkupBuilder to redirect output from standard output
to a file.  I.e. instead of:

        def xml = new groovy.xml.MarkupBuilder ( )

sending output to a file is achieved by...

Thanks.

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

tugwilson

On 21 Mar 2006, at 08:01, Russel Winder wrote:

> On Mon, 2006-03-20 at 21:27 +0000, John Wilson wrote:
>
>>> I have almost certainly just missed something obvious but is  
>>> there any
>>> easy idiomatic way of getting <?xml...?> and <!DOCTYPE...> tags  
>>> out of
>>> the groovy.xml.MarkupBuilder?  JDOM and dom4j make this quite easy.
>>
>> No but you can do it with StreamingMarkupBuilder.
>
> That seems a shame.  I tried replacing MarkupBuilder with Streaming
> MarkupBuilder in a script that worked but now it produces:
>
> Caught: groovy.lang.MissingMethodException: No signature of method
> groovy.xml.StreamingMarkupBuilder.contacts() is applicable for  
> argument
> types: (writeContacts_better$_run_closure1) values:
> {writeContacts_better$_run_closure1@c931fc}
>         at
> groovy.xml.streamingmarkupsupport.AbstractStreamingBuilder.invokeMetho
> d(AbstractStreamingBuilder.groovy)
>         at writeContacts_better.run(writeContacts_better.groovy:6)
>         at writeContacts_better.main(writeContacts_better.groovy)
>
> Which means that StreamingMarkupBuilder must be used very  
> differently to
> MarkupBuilder.  Google doesn't have any  StreamingMarkupBuilder  
> examples
> indexed from what I can see quickly.

Well there are example of the use in the unit tests and Google comes  
up with a reasonable number of examples.

My presentation to XML2004 linked here: http://groovy.codehaus.org/ 
Articles gives an extended example.

>
>> ther's a command called mkp.yieldUnescaped which lets you drop
>> arbitrary CDATA into XML documents so you can use that for the XML
>> declaration and the internal DTD subset.
>>
>> It lets lets you insert PIs and comments
>>
>> mkp.comment "this is a comment"
>>
>> mkp.pi("myTarget", "myInstruction")
>>
>> mkp.pi("myTarget", [name1: "value1", name2: "value2"])
>
> This sort of code doesn't work for me :-(

Details?

>
> As a side question:  I assume there is a parameter you can give in the
> constructor to the MarkupBuilder to redirect output from standard  
> output
> to a file.  I.e. instead of:
>
> def xml = new groovy.xml.MarkupBuilder ( )
>
> sending output to a file is achieved by...
>

I'm not sure i really don't use MarkupBuilder

with StreamingMarkupBuilder the process of binding the closure to the  
builder produces a Writable object so you can write it wherever you  
like.



John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

Russel Winder
On Tue, 2006-03-21 at 08:15 +0000, John Wilson wrote:

> > MarkupBuilder.  Google doesn't have any  StreamingMarkupBuilder  
> > examples
> > indexed from what I can see quickly.
>
> Well there are example of the use in the unit tests and Google comes  
> up with a reasonable number of examples.

I am being stupid, I should have looked in the unit tests.

You must be better at using Google than me as I found lots of references
to Fisheye but no useful things ;-)

> My presentation to XML2004 linked here: http://groovy.codehaus.org/ 
> Articles gives an extended example.

Excellent, thanks.

I now have a StreamingMarkupBuilder example working and with the use of
<<, all the C++ addicts in the audience of the talk will become very
happy :-)

> >> mkp.pi("myTarget", "myInstruction")
> >>
> >> mkp.pi("myTarget", [name1: "value1", name2: "value2"])
> >
> > This sort of code doesn't work for me :-(
>
> Details?

If I put lines like that in the closure being used in the builder.bind
call then I get no output.  I guess I am being slow this morning.

> I'm not sure i really don't use MarkupBuilder

Does anyone use MarkupBuilder or is StreamingMarkupBuilder now Groovy
standard?

> with StreamingMarkupBuilder the process of binding the closure to the  
> builder produces a Writable object so you can write it wherever you  
> like.

This makes sense to me and the examples will sit well with the C++
mindset.

Is XmlSlurper the Groovy standard reader or is there the same sort of
dichotomy we have with MarkupBuilder and StreamingMarkupBuilder ?

Thanks.

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

Russel Winder
In reply to this post by tugwilson
On Mon, 2006-03-20 at 21:27 +0000, John Wilson wrote:
> On 20 Mar 2006, at 21:03, Russel Winder wrote:
>
> > I have almost certainly just missed something obvious but is there any
> > easy idiomatic way of getting <?xml...?> and <!DOCTYPE...> tags out of
> > the groovy.xml.MarkupBuilder?  JDOM and dom4j make this quite easy.
>
> No but you can do it with StreamingMarkupBuilder.

Not totally true it seems.  MarkupBuilder can take a Writer as a
constructor parameter and then it writes to that stream.

writer = new BufferedWriter ( new FileWriter ( 'contacts.xml' ) )
def xml = new groovy.xml.MarkupBuilder ( writer )
def eol = System.properties.'line.separator'
writer << '<?xml version="1.0" encoding="UTF-8"?>' << eol
xml.contacts {
    contact {

etc.

Trivial but...

MarkupBuilder inserts appropriate indentation before each element and
appends an EOL after each element whereas StreamingMarkupBuilder does
not.  Irrelevant for general processing but not for creating human
readable files.  Is there a formatting option (a la dom4j etc.) that
creates human readable output with StreamingMarkupBuilder?

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

tugwilson

On 21 Mar 2006, at 09:24, Russel Winder wrote:

> Trivial but...
>
> MarkupBuilder inserts appropriate indentation before each element and
> appends an EOL after each element whereas StreamingMarkupBuilder does
> not.  Irrelevant for general processing but not for creating human
> readable files.  Is there a formatting option (a la dom4j etc.) that
> creates human readable output with StreamingMarkupBuilder?


MarkupBuilder cannot emit mixed content. I think that this is a  
pretty major deficiency. However as it can't ever produce mixed  
content then it can "pretty print" the output without screwing the  
document up.

StreamingMarkupBuilder can produce mixed content so it does not  
attempt to pretty print the output because doing so could break a  
document. I don't intend to add pretty printing as an option. I just  
load the output into an XMl editor (I use Oxygen) and use the  
formatting feature there if I want to see the structure.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

tugwilson
In reply to this post by Russel Winder

On 21 Mar 2006, at 09:07, Russel Winder wrote:

>
> If I put lines like that in the closure being used in the builder.bind
> call then I get no output.  I guess I am being slow this morning.
>


No I got the example wrong!

Here's a full scale example of the use

import groovy.xml.StreamingMarkupBuilder

class C {
static atomPerson = {name, url = null, email = null ->
   mkp.declareNamespace(atom: "http://purl.org/atom/ns#")

   atom.name name
   if (url != null) atom.url url
   if (email != null) atom.email email
}

static doc = new StreamingMarkupBuilder().bind {
   mkp.declareNamespace(atom: "http://purl.org/atom/ns#")

   atom.feed(version: ".3") {
     mkp.pi(target: "body")
     mkp.pi(target:  [name1: "value1", name2: "value2"])
     atom.title "Test Jobs"
     atom.link(href: "http://www.example.org", rel: "alternate",  
type: "text/html")
     atom.modified " 2005-10-11T18:30:02Z"
     atom.author {
       def person = atomPerson.clone()
       person.delegate = delegate
       person "John Wilson"
     }
     atom.id "tag:example.org"
     atom.entry {
       atom.title "Test Job - don't apply "
       atom.link(href: "http://www.example.org")
       atom.summary "This is a test please don't apply for this job"
       atom.id "tag:example.org"
       atom.issued "2005-10-13T18:30:02Z"
       atom.modified "2005-10-13T18:30:02Z"
     }
   }
}

static main(args) {
     System.out << doc
}
}


Produces this output (after pretty printing)

<atom:feed version=".3" xmlns:atom="http://purl.org/atom/ns#">
     <?target body?>
     <?target name1="value1" name2="value2"?>
     <atom:title>Test Jobs</atom:title>
     <atom:link href="http://www.example.org" type="text/html"  
rel="alternate"/>
     <atom:modified> 2005-10-11T18:30:02Z</atom:modified>
     <atom:author>
         <atom:name>John Wilson</atom:name>
     </atom:author>
     <atom:id>tag:example.org</atom:id>
     <atom:entry>
         <atom:title>Test Job - don't apply </atom:title>
         <atom:link href="http://www.example.org"/>
         <atom:summary>This is a test please don't apply for this  
job</atom:summary>
         <atom:id>tag:example.org</atom:id>
         <atom:issued>2005-10-13T18:30:02Z</atom:issued>
         <atom:modified>2005-10-13T18:30:02Z</atom:modified>
     </atom:entry>
</atom:feed>



John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

Russel Winder
In reply to this post by tugwilson
On Tue, 2006-03-21 at 09:34 +0000, John Wilson wrote:

> MarkupBuilder cannot emit mixed content. I think that this is a  
> pretty major deficiency. However as it can't ever produce mixed  
> content then it can "pretty print" the output without screwing the  
> document up.

This is certainly a problem for tasks like creating websites but not
really a problem for data transfer.

> StreamingMarkupBuilder can produce mixed content so it does not  
> attempt to pretty print the output because doing so could break a  
> document. I don't intend to add pretty printing as an option. I just  
> load the output into an XMl editor (I use Oxygen) and use the  
> formatting feature there if I want to see the structure.

I think that is a very sensible position and no problem.

Does XmlSlurper do validating parsing?  If it does, does it validate
against DTDs, XML Schemas, RelaxNG Schemas?

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: groovy.xml.MarkupBuilder

tugwilson

On 21 Mar 2006, at 10:43, Russel Winder wrote:

> Does XmlSlurper do validating parsing?  If it does, does it validate
> against DTDs, XML Schemas, RelaxNG Schemas?


The default is that it uses a non validating, namespace aware parser.  
There are constructor options which allow you to switch validation on  
and switch namespace awareness off. You can also supply an XMLReader  
or a SAXParser.

So if you have a parser which validates against W3C or RNG schemas  
then you can do validation with XMLSlurper.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


12
Loading...