{"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},"categories":[1],"tags":[9,194,197,198,193],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/514"}],"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}]}}