xmlslurper

Change XML tag name

孤街浪徒 提交于 2021-02-08 13:56:19
问题 I want to transform an XML document which I have parsed with XmlSlurper . The (identical) XML tag names should be replaced with the value of the id attribute; all other attributes should be dropped. Starting from this code: def xml = """<tag id="root"> | <tag id="foo" other="blah" more="meh"> | <tag id="bar" other="huh"/> | </tag> |</tag>""".stripMargin() def root = new XmlSlurper().parseText(xml) // Some magic here. println groovy.xml.XmlUtil.serialize(root) I want to get the following:

Groovy - XmlSlurper - find innermost element

丶灬走出姿态 提交于 2020-01-06 03:16:45
问题 I have the following xml: <vehicle> <car> <price>100</price> <price>200</price> </car> <car> <price>300</price> <price>400</price> </car> </vehicle> Given an xml, how can we get the innermost elements (in this case, all the <price> elements)? 回答1: Assuming you have the xml in a String xml , you should be able to do: List prices = new XmlSlurper().parseText( xml ).car.price*.text()​​ 回答2: thanks Tim for the answer. I just figured out the following works too. And is more generic: def document =

Groovy - XmlSlurper - find innermost element

青春壹個敷衍的年華 提交于 2020-01-06 03:16:04
问题 I have the following xml: <vehicle> <car> <price>100</price> <price>200</price> </car> <car> <price>300</price> <price>400</price> </car> </vehicle> Given an xml, how can we get the innermost elements (in this case, all the <price> elements)? 回答1: Assuming you have the xml in a String xml , you should be able to do: List prices = new XmlSlurper().parseText( xml ).car.price*.text()​​ 回答2: thanks Tim for the answer. I just figured out the following works too. And is more generic: def document =

GPath to find if a table header contains a matching string

不羁岁月 提交于 2020-01-06 02:30:33
问题 I'm parsing an HTML file into a well-formed XML document using NekoHTML parser. However I can't quite figure out the GPath so that I can identify the table that has the "Settings" string. def parser = new org.cyberneko.html.parsers.SAXParser() parser.setFeature('http://xml.org/sax/features/namespaces', false) def html = ''' <html> <title>Hiya!</title> </html> <body> <table> <tr> <th colspan='3'>Settings</th> <td>First cell r1</td> <td>Second cell r1</td> </tr> </table> <table> <tr> <th

GPath to find if a table header contains a matching string

二次信任 提交于 2020-01-06 02:30:26
问题 I'm parsing an HTML file into a well-formed XML document using NekoHTML parser. However I can't quite figure out the GPath so that I can identify the table that has the "Settings" string. def parser = new org.cyberneko.html.parsers.SAXParser() parser.setFeature('http://xml.org/sax/features/namespaces', false) def html = ''' <html> <title>Hiya!</title> </html> <body> <table> <tr> <th colspan='3'>Settings</th> <td>First cell r1</td> <td>Second cell r1</td> </tr> </table> <table> <tr> <th

Groovy Node.depthFirst() returning a List of Nodes and Strings?

大城市里の小女人 提交于 2020-01-01 10:48:33
问题 I'm hoping someone will just point out something obvious that I'm missing here. I feel like I've done this a hundred times and for some reason tonight, the behavior coming from this is throwing me for a loop. I'm reading in some XML from a public API. I want to extract all the text from a certain node (everything within 'body'), which also includes a variety of child nodes. Simple example: <xml> <metadata> <article> <body> <sec> <title>A Title</title> <p> This contains <italic>italics</italic

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

拟墨画扇 提交于 2020-01-01 01:09:15
问题 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? 回答1: The API is a little screwy, but I think that there are a couple of better ways to look for children. What you're getting when you ask for "xml.bar" (which exists) or "xml.quux" which doesn't, is a groovy.util.slurpersupport.NodeChildren object. Basically a collection of nodes meeting the criteria that

Groovy XmlSlurper vs XmlParser

江枫思渺然 提交于 2019-12-29 02:39:48
问题 I searched for a while on this topic and found some results too, which I am mentioning at the end of post. Can someone help me precisely answer these three questions for the cases listed below them? For which use-cases using XmlSluper makes more sense than XmlParser and vice-versa (from point of view ease of use of API/Syntax)? Which one is more memory efficient? (looks like Slurper) which one processes the xml faster? Case a. when I have to read almost all nodes in the xml? Case b. when I

Parse xml using XmlSlurper in Groovy

左心房为你撑大大i 提交于 2019-12-25 07:48:25
问题 I have an xml response which looks like :- <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <multiRef xmlns:ns9="http://hero.ar.vixo.in" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns9:IdentityModel">

How to parse XML comments in Groovy?

北慕城南 提交于 2019-12-24 19:07:15
问题 Is there any way to parse XML comments in Groovy? Both XMLParser and XMLSluprer don't seem to support comments nodes. Suppose following file (example.html): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>title</title> <body> <table cellpadding="1"