xml - How can I check for the existence of an element with Groovy's XmlSlurper? -


I am trying to determine whether an XML element exists with Groovy's XmlSlurper Is there any way of doing it? For example:

  & lt; Foo & gt; & Lt; Bar / & gt; & Lt; / Foo & gt;  

How do I check that the bar element is present?

The API is a little blurry, but I think there are some good ways to search for kids. When you ask for "xml.bar" (which exists) or "xml.quux" what are you getting, what is an object is basically a collection of nodes that meets the criteria you asked for is.

Checking the size of the node channel is one way of looking at the expected size:

  def text = "  & Lt; / foo & gt; Def xml = new XmlSlurper (). Insert on ParseText (text) 1 == xml.bar.size () 0 == xml.quux.size ()  

Another method would be to use the search method and see Does the node name come back (unfortunately something always comes back), which you were hoping for:

  def text = "  

Comments