xmlslurper

How to read the hyphenated attribute names (Eg. model_name) while parsing xml using XmlSlurper

冷暖自知 提交于 2019-12-08 18:46:54
问题 I am trying to read an attribute while parsing XML using XmlSlurper in Groovy. When I try to read the hyphenated attribute model-number I am getting an exception. <router name="b" id="x" manufacturer-id="e" model-number="a"/> 回答1: def a = "<router name='b' id='x' manufacturer-id='e' model-number='a'/>" def router = new XmlSlurper().parseText(a) println router.@'manufacturer-id' println router.@'name' println router.@'id' println router.@'model-number' i tried this on console and it is working

How keep groovy/XMLSlurper from stripping html tags from a node?

北城余情 提交于 2019-12-08 06:42:52
问题 I'm reading an HTML file from a POST response and parsing it with XMLSlurper. The textarea node on the page has some HTML code put into it (non-urlencoded - not my choice) and when I read that value, Groovy strips all the tags. Example: <html> <body> <textarea><html><body>This has html code for some reason</body></html></textarea> </body> </html> When I parse the above and then find(...) the "textarea" node, it returns to me: This has html code for some reason and none of the tags. How do I

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

只谈情不闲聊 提交于 2019-12-06 23:23:16
问题 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>Telephone number: <strong>telephone</strong></span></address></li> <li><h3><a class="box" href="#href2">href2 link text</a> <span>extra stuff</span></h3><address

groovy Print path and value of elements in xml

只谈情不闲聊 提交于 2019-12-06 04:21:21
I have the following xml: <list> <cars> <model>2012</model> <make>GM</make> </cars> </list> I want to print these values as path:value as shown below. list/cars/model : 2012 list/cars/make : GM How can I achieve this? I tried the name() method but it only prints the name of child item. I want to print the whole path till the element. I can only use xmlSlurper parser to do this. Thanks. import groovy.util.XmlSlurper import groovy.util.slurpersupport.NodeChild; def rootNode = new XmlSlurper().parseText('<root><one a1="uno!"/><two>Some text!</two></root>' ) def printMap printMap = {node, path->

How can I use relative paths to external response files for soapUI MockService

孤街醉人 提交于 2019-12-06 01:49:41
问题 What I've Done I am using soapUI (3.6.1 Free version) mock services to serve up specific data to 2 client applications I am testing. With some simple Groovy script I've set up some mock operations to fetch responses from specific files based on the requests made by the client applications. The static contents of the mock response is: ${responsefile} The groovy in the operation dispatch scripting pane is: def req = new XmlSlurper().parseText(mockRequest.requestContent) if (req =~ "CategoryA")

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

泪湿孤枕 提交于 2019-12-05 20:54:15
问题 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 { it.@tags != null } because it finds all the nodes. 回答1: Try the following (Groovy 1.5.6): def doc = """ <html> <body> <div tags="1">test1</div> <div>test2</div> <div tags="">test3</div> <div tags="4">test4</div> </body> </html> """ def html = new XmlSlurper()

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

╄→尐↘猪︶ㄣ 提交于 2019-12-05 03:56:05
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>Telephone number: <strong>telephone</strong></span></address></li> <li><h3><a class="box" href="#href2">href2 link text</a> <span>extra stuff</span></h3><address>Here is another address<span>Another telephone: <strong>0845 1111111</strong></span></address></li> <

How can I use relative paths to external response files for soapUI MockService

北城余情 提交于 2019-12-04 06:38:34
What I've Done I am using soapUI (3.6.1 Free version) mock services to serve up specific data to 2 client applications I am testing. With some simple Groovy script I've set up some mock operations to fetch responses from specific files based on the requests made by the client applications. The static contents of the mock response is: ${responsefile} The groovy in the operation dispatch scripting pane is: def req = new XmlSlurper().parseText(mockRequest.requestContent) if (req =~ "CategoryA") { context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryA.xml").text } else {

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

狂风中的少年 提交于 2019-12-04 03:04:54
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 { it.@tags != null } because it finds all the nodes. Try the following (Groovy 1.5.6): def doc = """ <html> <body> <div tags="1">test1</div> <div>test2</div> <div tags="">test3</div> <div tags="4">test4</div> </body> </html> """ def html = new XmlSlurper().parseText( doc) html.body.div.findAll { it.@tags.text()}.each { div -> println div.text() } This outputs: test1

How to remove an element in Groovy using XmlSlurper?

给你一囗甜甜゛ 提交于 2019-11-29 11:15:17
For example, how can I remove all tags with name one in rootNode programmatically? def rootNode = new XmlSlurper().parseText( '<root><one a1="uno!"/><two>Some text!</two></root>' ) I tried rootNode.children().removeAll{ it.name() == 'one' } but it reported: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.removeAll() is applicable for argument types: (DUMMY$_closure1_closure2) values: [DUMMY$_closure1_closure2@6c5f92d3] Try rootNode.one.replaceNode { } To complete the answer: def rootNode = new XmlSlurper().parseText ( '<root><one a1="uno!"/>