gpath

GPathResult to String without XML declaration

烂漫一生 提交于 2021-02-08 13:48:23
问题 I'm converting GPathResult to String using def gPathResult = new XmlSlurper().parseText('<node/>') XmlUtil.serialize(gPathResult) It works fine, but I'm getting XML declaration in front of my XML <?xml version="1.0" encoding="UTF-8"?><node/> How can I convert GPathResult to String without <?xml version="1.0" encoding="UTF-8"?> at the beginning? 回答1: Use XmlParser instead of XmlSlurper : def root = new XmlParser().parseText('<node/>') new XmlNodePrinter().print(root) Using new XmlNodePrinter

Way to deep traverse a Groovy object with dot in string using GPath

蹲街弑〆低调 提交于 2020-01-11 06:07:56
问题 The situation I have is that I'm querying MongoDB with a string for a field that is more than one level deep in the object hierarchy. This query must be a string. So for example I'm querying for something like this in Groovy: def queryField = 'a.b.c' //this is variable and can be different every time def result = mongodb.collection.findOne([queryField:5]) The problem no arises that in the result I want to find the value of the nested field. With GPath I could go one level deep and get a's

Way to deep traverse a Groovy object with dot in string using GPath

南楼画角 提交于 2020-01-11 06:07:31
问题 The situation I have is that I'm querying MongoDB with a string for a field that is more than one level deep in the object hierarchy. This query must be a string. So for example I'm querying for something like this in Groovy: def queryField = 'a.b.c' //this is variable and can be different every time def result = mongodb.collection.findOne([queryField:5]) The problem no arises that in the result I want to find the value of the nested field. With GPath I could go one level deep and get a's

Way to deep traverse a Groovy object with dot in string using GPath

旧街凉风 提交于 2020-01-11 06:05:49
问题 The situation I have is that I'm querying MongoDB with a string for a field that is more than one level deep in the object hierarchy. This query must be a string. So for example I'm querying for something like this in Groovy: def queryField = 'a.b.c' //this is variable and can be different every time def result = mongodb.collection.findOne([queryField:5]) The problem no arises that in the result I want to find the value of the nested field. With GPath I could go one level deep and get a's

Way to deep traverse a Groovy object with dot in string using GPath

不羁的心 提交于 2020-01-11 06:05:44
问题 The situation I have is that I'm querying MongoDB with a string for a field that is more than one level deep in the object hierarchy. This query must be a string. So for example I'm querying for something like this in Groovy: def queryField = 'a.b.c' //this is variable and can be different every time def result = mongodb.collection.findOne([queryField:5]) The problem no arises that in the result I want to find the value of the nested field. With GPath I could go one level deep and get a's

How to apply predicate to GPath when I am using XMLSlurper

孤者浪人 提交于 2019-12-11 20:37:17
问题 lets say I have the following XML <InvoiceLines> <InvoiceLine> <InsertionDate>29.01.2015</InsertionDate> <Productnbr>0102</Productnbr> </InvoiceLine> <InvoiceLine> <InsertionDate>29.02.2015</InsertionDate> <Productnbr>0103</Productnbr> </InvoiceLine> </InvoiceLines> and I would like to get the insertion date of InvoiceLine that has Productnbr = 0103. If I would write xpath I would write somthing like: //InvoiceLine[./Productnbr='0103']/InsertionDate but I would like to use GPath since I am

XmlSlurper in Groovy Script — Insert nodes to GpathResult using external closures

跟風遠走 提交于 2019-12-11 18:02:56
问题 I have a problem with the below scenario: -- I have a GPathResult "body" to which I want to append some more xml (nodes and children) -- Some parts are common so I am trying to have them kept in an outer closure "commonNode" I can insert wherever I need // some more code here to get body def commonNode = { return { node2() { child("childValue") } } } body.appendNode( { node1("value1") commonNode() node3("value3") } ) What I want to get after I would call XmlUtil.serialize(body) is this: ...

groovy Print path and value of elements in xml

断了今生、忘了曾经 提交于 2019-12-10 10:53:29
问题 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. 回答1: import groovy.util.XmlSlurper import groovy.util.slurpersupport.NodeChild; def rootNode = new

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 to find element by attribute value in GPath?

假如想象 提交于 2019-12-05 09:57:48
问题 What is an alternative to this XPath //div[@id='foo'] in GPath? In general, where I can find this documentation? 回答1: Here is the corresponding snippet: def node = new XmlSlurper().parseText(...) def foo = node.depthFirst().findAll { it.name() == 'div' && it.@id == 'foo'} A few other links you may want to read: GPath documentation Processing XML with Groovy 回答2: The previous poster gave you all that's required: Assuming your document has been slurped into xml , you want def foo = xml.path.to