You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Kenton Garner <ke...@issinc.com> on 2016/04/05 20:15:06 UTC

Can anyone comment on this stackoverflow posting that indicates XmlSlurper is actually namespace aware by default

http://stackoverflow.com/questions/33418826/are-xmlparser-and-xmlslurper-namespace-aware-by-default#

This has been my experience as well, but perhaps I am confused.

I would like to parse an XML file and reference nodes without specifying the namespace prefix, this only works if I set the
XmlSlurper namespace aware parameter to "true" which seems backwards to me.

-garneke



Re: Can anyone comment on this stackoverflow posting that indicates XmlSlurper is actually namespace aware by default

Posted by John Wagenleitner <jo...@gmail.com>.
Setting namespaceAware to true (default) means that it provides support for
namespaces, so in your sampleDoc it recognizes that an element such as
"xact:update" is actually "{com.here.repl}update" instead of just
"xact:update".  Usually this would be used together with declareNamespace
since the important information is the URI and you wouldn't want to have to
rely on the prefix always being the same.

For example:

    def xml  = new
XmlSlurper(false,true).parseText(sampleDoc).declareNamespace(a:
'com.here.repl', b: 'com.here.repl.data');
    assert xml.'a:update'.'b:table'.'b:after','b:CLASSIFICATION'.text() ==
'UNCLASSIFIED'
    assert xml.'update'.'table'.'after','CLASSIFICATION'.text() ==
'UNCLASSIFIED'

Since it's namespaceAware you can even forgo including the namespace as
long as it's not required to uniquely identify the element.

Setting namespaceAware to false means the parser wont recognize namespaces
so it expects you to access the element by the full name "xact:update".  On
a non-namespaceAware parser using the declareNamespace wont have any affect.




On Tue, Apr 5, 2016 at 1:15 PM, Winnebeck, Jason <
Jason.Winnebeck@windstream.com> wrote:

> After seeing this a few times now, including being bit by it myself and my
> work team, I have submitted a JIRA and PR to remove the 4 confusing
> characters from the Javadoc :).
>
> https://issues.apache.org/jira/browse/GROOVY-7810  XmlSluper default
> constructor documentation about namespace aware incorrect.
> https://github.com/apache/groovy/pull/305  GROOVY-7810 fix XmlSlurper
> default ctor doc
>
> I posted answer to SO explaining the situation
> http://stackoverflow.com/a/36435993/3875382
>
> Jason
>
> -----Original Message-----
> From: garneke [mailto:kenton.garner@issinc.com]
> Sent: Tuesday, April 05, 2016 3:59 PM
> To: users@groovy.incubator.apache.org
> Subject: Re: Can anyone comment on this stackoverflow posting that
> indicates XmlSlurper is actually namespace aware by default
>
> Here is an unsophisticated example…
>
> testparse.groovy
> <http://groovy.329449.n5.nabble.com/file/n5732207/testparse.groovy>
>
>
>
> --
> View this message in context:
> http://groovy.329449.n5.nabble.com/Can-anyone-comment-on-this-stackoverflow-posting-that-indicates-XmlSlurper-is-actually-namespace-awat-tp5732204p5732207.html
> Sent from the Groovy Users mailing list archive at Nabble.com.
>
> ----------------------------------------------------------------------
> This email message and any attachments are for the sole use of the
> intended recipient(s). Any unauthorized review, use, disclosure or
> distribution is prohibited. If you are not the intended recipient, please
> contact the sender by reply email and destroy all copies of the original
> message and any attachments.
>

RE: Can anyone comment on this stackoverflow posting that indicates XmlSlurper is actually namespace aware by default

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
After seeing this a few times now, including being bit by it myself and my work team, I have submitted a JIRA and PR to remove the 4 confusing characters from the Javadoc :).

https://issues.apache.org/jira/browse/GROOVY-7810  XmlSluper default constructor documentation about namespace aware incorrect.
https://github.com/apache/groovy/pull/305  GROOVY-7810 fix XmlSlurper default ctor doc

I posted answer to SO explaining the situation http://stackoverflow.com/a/36435993/3875382

Jason

-----Original Message-----
From: garneke [mailto:kenton.garner@issinc.com] 
Sent: Tuesday, April 05, 2016 3:59 PM
To: users@groovy.incubator.apache.org
Subject: Re: Can anyone comment on this stackoverflow posting that indicates XmlSlurper is actually namespace aware by default

Here is an unsophisticated example…

testparse.groovy
<http://groovy.329449.n5.nabble.com/file/n5732207/testparse.groovy>  



--
View this message in context: http://groovy.329449.n5.nabble.com/Can-anyone-comment-on-this-stackoverflow-posting-that-indicates-XmlSlurper-is-actually-namespace-awat-tp5732204p5732207.html
Sent from the Groovy Users mailing list archive at Nabble.com.

----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.

Re: Can anyone comment on this stackoverflow posting that indicates XmlSlurper is actually namespace aware by default

Posted by garneke <ke...@issinc.com>.
Here is an unsophisticated example…

testparse.groovy
<http://groovy.329449.n5.nabble.com/file/n5732207/testparse.groovy>  



--
View this message in context: http://groovy.329449.n5.nabble.com/Can-anyone-comment-on-this-stackoverflow-posting-that-indicates-XmlSlurper-is-actually-namespace-awat-tp5732204p5732207.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: Can anyone comment on this stackoverflow posting that indicates XmlSlurper is actually namespace aware by default

Posted by John Wagenleitner <jo...@gmail.com>.
I think the SO answer is correct that the documentation is incorrect.
Originally the javadoc stated:

    "Uses the defaults of not validating and namespace aware."

Some javadoc updates were made since and it was probably assumed that "not"
also applied to namespace aware.

Do you have any code sample you can share that shows where you have to set
the namespace aware parameter to true in order to access without a
namespace?


On Tue, Apr 5, 2016 at 11:15 AM, Kenton Garner <ke...@issinc.com>
wrote:

>
> http://stackoverflow.com/questions/33418826/are-xmlparser-and-xmlslurper-namespace-aware-by-default#
>
>
>
> This has been my experience as well, but perhaps I am confused.
>
>
>
> I would like to parse an XML file and reference nodes without specifying
> the namespace prefix, this only works if I set the
>
> XmlSlurper namespace aware parameter to “true” which seems backwards to me.
>
>
>
> -garneke
>
>
>
>
>