xmlslurper

Groovy: Correct Syntax for XMLSlurper to find elements with a given attribute

Given a HTML file with the structure html->body->a bunch of divs what is the correct groovy statement to find all of the divs with a non blank tags attribute? The following is not working: def nodes = html.body.div.findAll {[email protected] != null} because it finds all the nodes. ...

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

I'm trying to determine whether an XML element exists with Groovy's XmlSlurper. Is there a way to do this? For example: <foo> <bar/> </foo> How do I check whether the bar element exists? ...

Can generic XML by parsed as nicely as simple XML in Groovy?

Given a nice, simple XML structure, XmlSlurper() can allow me to read values from it very easily. def xml = "<html><head><title>groovy</title></head></html>" def html = new XmlSlurper().parseText(xml) println html.head.title Is there a way to make this simple tree navigation possible for generic (type-based, etc) XML. Ideally, in t...

Using XmlSlurper: How to select sub-elements while iterating over a GPathResult

I am writing an HTML parser, which uses TagSoup to pass a well-formed structure to XMLSlurper. Here's the generalised code: def htmlText = """ <html> <body> <div id="divId" class="divclass"> <h2>Heading 2</h2> <ol> <li><h3><a class="box" href="#href1">href1 link text</a> <span>extra stuff</span></h3><address>Here is the address<span>Te...

groovy XmlSlurper not parse my xml file

I have an xml, and I can't parse this file with xmlslurper. Here a copy of my xml file : <Entrezgene-Set> <Entrezgene> <Entrezgene_summary>The protein encoded by this gene is a plasma glycoprotein of unknown function. The protein shows sequence similarity to the variable regions of some immunoglobulin supergene family member proteins. [...

Rendering XML response on Grails problem

Hello, I'm having problems rendering an XML file parsed via XMLSlurper to an XML variable. Below is my code. def userFile =new File("test.xml") def xml= new XmlSlurper().parse(userFile) render xml The problem is I cannot see the xml being rendered into my flex app. ...

Using XmlSlurper in Groovy / Grails to parse a Pingdom XML response

I have used XmlSlurper successfully before, but am struggling to parse the following XML - it is a response from a call to the Pingdom API. I have tried following the namespace declaration examples, but I just get an error message on the ns1 and ns2 values. Can anybody help point me in the right direction? The xml looks like this:- <?x...

mkp.yieldUnescaped not working

Hi all. I am really stuck here. I am creating an XML document with Groovy 1.7 and everything is working except one section is being escaped when it shouldn't. I am starting out like this: triadDoc = new XmlSlurper().parse(xmlTriadMessageDocumentPath) writer = new StringWriter() xmlBuilder = new Stre...

Groovier way to find next XmlSlurper element in parent?

Dear All: I am using: def idx=parent.item.children().indexOf(myElement) if (idx+1<parent.children().size()) { def message=parent.children()[idx+1] println message.text() } To find the element message which is next after myelement in the parent. However, it seems there must be a Groovier way, no? Thank you Misha ...

XmlSlurper/NekoHTML document fragment parsing - No HTML or BODY tags wanted

Dear All, I am trying to parse the following HTML fragment, and I would like to get the same fragment as output (without HTML and BODY tags). Is this possible? If so, how? Thank you Misha p.s. I am reading here: http://nekohtml.sourceforge.net/faq.html#fragments and I believe I have added the correct options below. However, the output ...

XmlSlurper NodeChild that represents a body tag?

Dear All: I would like to get a Groovy NodeChild that represents, e.g., a body tag. However, if I do html=new XmlSlurper().parseText(blah) I get html which is a NodeChild. However html.body is a NodeChildren tag, and I can't seem to get a NodeChild. Much help appreciated! Thank you Misha ...

Groovy pretty print XmlSlurper output from HTML?

Dear All: I am using several different versions to do this but all seem to result in this error: [Fatal Error] :1:171: The prefix "xmlns" cannot be bound to any namespace explicitly; neither can the namespace for "xmlns" be bound to any prefix explicitly. I load html as: // Load html file def fis=new FileInputStream("2.html") def htm...

replace XmlSlurper tag with arbitrary XML

Dear All: I am trying to replace specific XmlSlurper tags with arbitrary XML strings. The best way I have managed to come up with to do this is: #!/usr/bin/env groovy import groovy.xml.StreamingMarkupBuilder def page=new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parseText(""" <html> <head></head> <body> <one attr1='val1'...

Grails/Groovy problem with XmlSlurper .. help ..

Using grails 1.2.2 with groovy 1.6.8 .. Reading a a web service and trying to process response .. Response is shown below and validates as correct xml .. (sorry for length) .. <soap:Body> <AddProductEventResponse xmlns="http://tempuri.org/"&gt; <AddProductEventResult> <xs:schema id="AddProductEventResult" xmlns=""...

Groovy delete a tag in a XMLSlurper, replaceNode {} does nothing

I am parsing some XML with XMLSlurper (groovy 1.7.4) and I need to delete a tag (not make it empty!). Here is a code sample illustrating it: import groovy.xml.StreamingMarkupBuilder def CAR_RECORDS = ''' <records> <car name='HSV Maloo' make='Holden' year='2006'> <country>Australia</country> <record type='speed...

Sorting XML in Groovy

I have looked at the documentation on sorting XML with Groovy def records = new XmlParser().parseText(XmlExamples.CAR_RECORDS) assert ['Royale', 'P50', 'HSV Maloo'] == records.car.sort{ it.'@year'.toInteger() }.'@name' but what I am trying to do is sort the XML and then return the xml string sorted. I know I can completely rebuild the...

Groovy Xml Parsing with namespaces

Hi, I've been trying to do some xml modifications with groovy's XML Slurper. Basically, i'm going through the xml and looking for tags or attributes that have ? as the value and then replacing it with some value. I've got it working for xml that doesn't have namespaces but once I include them things get wonky. For example, this: ...

Groovy XMLSlurper issue

I want to parse with XmlSlurper a HTML document which I read using HTTPBuilder. Initialy I tried to do it this way: def response = http.get(path: "index.php", contentType: TEXT) def slurper = new XmlSlurper() def xml = slurper.parse(response) But it produces an exception: java.io.IOException: Server returned HTTP response code: 503 ...

Iterate all children with a given name using GPathResult returned by XmlSlurper

I've parsed some html using XmlSlurper. Now I want to iterate all the children with a given element name. What I've got now is the following code snippet html.'**'.findAll { it.name() == 'a' }.each { println it } It works but just isn't groovy enough. I would like to simply write something like this html.'**'...