{"id":3725,"date":"2011-01-19T13:28:00","date_gmt":"2011-01-19T13:28:00","guid":{"rendered":""},"modified":"2026-04-18T22:52:21","modified_gmt":"2026-04-18T22:52:21","slug":"groovy-xml-script-that-is-copying-elements-and-changing-attribute-values","status":"publish","type":"post","link":"https:\/\/hellem.org\/wblog\/?p=3725","title":{"rendered":"Groovy XML-script that is copying elements and changing attribute values"},"content":{"rendered":"<p>Based on all the good examples provided by http:\/\/groovy.codehaus.org\/ I&#8217;v managed to create a Groovy script that do<\/p>\n<ul>\n<li>Find all elements with a name attribute prefixed with &#8216;DEV_&#8217;<\/li>\n<li>For all found elements, search for element with identical name attribute, but without prefix &#8216;DEV_&#8217;. If element found, remove it<\/li>\n<li>Create a copy of the element with the &#8216;DEV_&#8217; prefix and then\n<ul>\n<li>Remove the prefix<\/li>\n<li>Change the value of the attribute driver<\/li>\n<li>Add the copy to the list of cars<\/li>\n<\/ul>\n<\/li>\n<li>Write the updated XML to file<\/li>\n<\/ul>\n<p>Click more to view the script. Sorry for the limited display of code in my blog.<br \/>\n[teaserbreak]<br \/>\n<!-- codeblock lang=\"\" line=\"1\" --><\/p>\n<pre><code>\r\nimport groovy.xml.DOMBuilder\r\nimport groovy.xml.dom.DOMCategory\r\nimport groovy.xml.dom.DOMUtil\r\n\r\nclass CarExamples {\r\nstatic def CAR_RECORDS = '''\r\n    &lt;records&gt;\r\n      &lt;car name='HSV Maloo' make='Holden' year='2006'&gt;\r\n        &lt;country&gt;Australia&lt;\/country&gt;\r\n        &lt;record type='speed'&gt;Production Pickup Truck with speed of 271kph&lt;\/record&gt;\r\n      &lt;\/car&gt;\r\n      &lt;car name='P50' make='Peel' year='1962'&gt;\r\n        &lt;country&gt;Isle of Man&lt;\/country&gt;\r\n        &lt;record type='size'&gt;Smallest Street-Legal Car at 99cm wide and 59 kg in weight&lt;\/record&gt;\r\n      &lt;\/car&gt;\r\n      &lt;car name='Royale' make='Bugatti' year='1931'&gt;\r\n        &lt;country&gt;France&lt;\/country&gt;\r\n        &lt;record type='price'&gt;Most Valuable Car at $15 million&lt;\/record&gt;\r\n      &lt;\/car&gt;\r\n    &lt;\/records&gt;\r\n  '''}\r\n\r\ndef reader  = new StringReader(CarExamples.CAR_RECORDS)\r\ndef doc     = DOMBuilder.parse(reader)\r\ndef records = doc.documentElement\r\n\r\nuse (DOMCategory) {\r\n\r\n\tprintln \"There are \" + records.'*'.size() + \" cars defined in the file\"\r\n    def cars = records.'car'\r\n\tprintln \"The parent element for cars are \" + cars[0].parent().getTagName()\r\n    def dev_cars = cars.findAll{it.\"@name\".startsWith(\"DEV_\")}\r\n\tprintln \" \"\r\n\tdev_cars.each {\r\n\t\tdef nameofdev = it.\"@name\"\r\n\t\tprintln \"Dev name: \" + nameofdev\r\n\t\tdef stringArr = nameofdev.split(\"DEV_\")\r\n\t\tdef deleteMeName = stringArr[1]\r\n\t\tprintln \"Node to remove has name: \" + deleteMeName\r\n\t\tdef deleteRecord = cars.find{ it.'@name' == deleteMeName }\r\n\t\tif (null != deleteRecord) {\r\n\t\t\trecords.removeChild(deleteRecord)\r\n\t\t}\r\n\t\tdef copyOfDev = it.cloneNode(true)\r\n\t\tcopyOfDev.setAttribute(\"name\", deleteMeName)\r\n\t\tcopyOfDev.setAttribute(\"driver\", \"racer\")\r\n\t\trecords.appendChild(copyOfDev)\r\n\t\tprintln \"\\n\"\r\n\t}\r\n}\r\n\r\noutputFile = new File(\"resultfile.xml\")\r\noutputFile.write(DOMUtil.serialize(records))\r\n\r\n<\/code><\/pre>\n<p><!-- \/codeblock --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Based on all the good examples provided by http:\/\/groovy.codehaus.org\/ I&#8217;v managed to create a Groovy script that do Find all elements with a name attribute prefixed with &#8216;DEV_&#8217; For all found elements, search for element with identical name attribute, but without prefix &#8216;DEV_&#8217;. If element found, remove it Create a copy of the element with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-3725","post","type-post","status-publish","format-standard","hentry","category-work-geek"],"_links":{"self":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3725"}],"version-history":[{"count":1,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3725\/revisions"}],"predecessor-version":[{"id":13876,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3725\/revisions\/13876"}],"wp:attachment":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}