{"id":514,"date":"2010-03-26T11:12:12","date_gmt":"2010-03-26T05:42:12","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=514"},"modified":"2010-03-26T11:12:12","modified_gmt":"2010-03-26T05:42:12","slug":"xmlslurper-made-it-simple","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/","title":{"rendered":"XmlSlurper made it simple"},"content":{"rendered":"<p>Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we wrote:<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">import org.ccil.cowan.tagsoup.*\r\nimport groovy.xml.*\r\nimport groovy.util.XmlSlurper\r\nimport javax.xml.parsers.SAXParser\r\n\r\npublic String parseAndUpdateHtml(Map params, String htmlContent) {\r\n    htmlContent = htmlContent.replaceAll('selected=\"selected\"', \"\")  \/\/to remove already selected values in select box\r\n    htmlContent = htmlContent.replaceAll('checked=\"checked\"', \"\")  \/\/to remove already selected values in check box\r\n\r\n    @Grab(group = 'org.ccil.cowan.tagsoup', module = 'tagsoup', version = '1.2')\r\n\/\/ Define our TagSoup backed parser\r\n    def slurper = new XmlSlurper()\r\n\/\/ Parse our html\r\n    def html = slurper.parseText(htmlContent)\r\n\r\n    params.each {id, value ->\r\n      def htmlBody = html.body.\"**\"\r\n      def input = htmlBody.find { it.@id == id }   \/\/name and id for all text boxes and select boxes were kept same\r\n      if (input) {\r\n        if (id.startsWith(\"txt\")) {   \/\/ text box names had a prefix 'txt'\r\n          input.@value = value\r\n        } else if (id.startsWith(\"cmb\")) {     \/\/ select box names had a prefix 'cmb'\r\n          def option = input.option.list().find {it == value}\r\n          if (option)\r\n            option.@selected = \"selected\"\r\n        }\r\n      }\r\n    }\r\n\r\n    params.each {name, value ->\r\n      if (name.startsWith(\"cb\") && value) {     \/\/ check box names had a prefix 'cb'\r\n          def chkBoxvalues = [value].flatten()\r\n          chkBoxvalues.each {chkBoxValue ->\r\n          def htmlBody = html.body.\"**\"\r\n          def input = htmlBody.find { it.@type == \"checkbox\" && it.@name == name && it.@value == chkBoxValue }\r\n          if (input) {\r\n            input.@checked = \"checked\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\n\/\/ Write it out (into a StringWriter for now)\r\n    def w = new StringWriter()\r\n    w << new StreamingMarkupBuilder().bind {\r\n      \/\/ Required to avoid the html: namespace on every node\r\n      mkp.declareNamespace '': 'http:\/\/www.w3.org\/1999\/xhtml'\r\n      mkp.yield html\r\n    }\r\n\/\/ XmlUtil.serialize neatens up our resultant xml -- but adds an xml declaration :-(\r\n    return new XmlUtil().serialize(w.toString())\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Thanks to  <a href=\"http:\/\/stackoverflow.com\/questions\/2510536\/html-parsing-fetch-and-update-data-from-the-html-file\">tim_yates<\/a> and others who helped me with this.<\/p>\n<p>Hope this helped!<\/p>\n<p>Regards,<br \/>\n~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nIntelliGrape Software<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[1],"tags":[9,194,197,198,193],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-technology","tag-groovy","tag-html-parsing","tag-select-box","tag-tagsoup","tag-xmlslurper"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Amit Jain\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"XmlSlurper made it simple | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"XmlSlurper made it simple | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#article\",\"name\":\"XmlSlurper made it simple | TO THE NEW Blog\",\"headline\":\"XmlSlurper made it simple\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2010-03-26T11:12:12+05:30\",\"dateModified\":\"2010-03-26T11:12:12+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#webpage\"},\"articleSection\":\"Technology, Groovy, html parsing, select box, tagsoup, xmlslurper\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#listItem\",\"name\":\"XmlSlurper made it simple\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#listItem\",\"position\":3,\"name\":\"XmlSlurper made it simple\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/\",\"name\":\"Amit Jain\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/1cbbcbd3-718c-4d6d-829c-4e225ae91a1a_amit.jain@tothenew.com.jpg\",\"width\":96,\"height\":96,\"caption\":\"Amit Jain\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/\",\"name\":\"XmlSlurper made it simple | TO THE NEW Blog\",\"description\":\"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\\\/select\\\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/xmlslurper-made-it-simple\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/amit\\\/#author\"},\"datePublished\":\"2010-03-26T11:12:12+05:30\",\"dateModified\":\"2010-03-26T11:12:12+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"XmlSlurper made it simple | TO THE NEW Blog","description":"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we","canonical_url":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#article","name":"XmlSlurper made it simple | TO THE NEW Blog","headline":"XmlSlurper made it simple","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/amit\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2010-03-26T11:12:12+05:30","dateModified":"2010-03-26T11:12:12+05:30","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#webpage"},"articleSection":"Technology, Groovy, html parsing, select box, tagsoup, xmlslurper"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/www.tothenew.com\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#listItem","name":"XmlSlurper made it simple"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#listItem","position":3,"name":"XmlSlurper made it simple","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/amit\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/amit\/","name":"Amit Jain","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/1cbbcbd3-718c-4d6d-829c-4e225ae91a1a_amit.jain@tothenew.com.jpg","width":96,"height":96,"caption":"Amit Jain"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/","name":"XmlSlurper made it simple | TO THE NEW Blog","description":"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/amit\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/amit\/#author"},"datePublished":"2010-03-26T11:12:12+05:30","dateModified":"2010-03-26T11:12:12+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"XmlSlurper made it simple | TO THE NEW Blog","og:description":"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we","og:url":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"XmlSlurper made it simple | TO THE NEW Blog","twitter:description":"Hi! We had a myForm.html file (accessible only from the third party) where in we needed to update the form containing input\/select\/check boxes with the values we had in the request params and then render it. So we used xmlslurper for the same. Without much effort, it just worked great. Following is the code we","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"514","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 16:40:01","updated":"2024-02-29 09:27:25","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tXmlSlurper made it simple\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Technology","link":"https:\/\/www.tothenew.com\/blog\/category\/technology\/"},{"label":"XmlSlurper made it simple","link":"https:\/\/www.tothenew.com\/blog\/xmlslurper-made-it-simple\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=514"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}