{"id":1370,"date":"2011-10-05T18:15:05","date_gmt":"2011-10-05T17:15:05","guid":{"rendered":"http:\/\/thekua.com\/atwork\/?p=1370"},"modified":"2019-07-18T16:08:09","modified_gmt":"2019-07-18T14:08:09","slug":"smooth-scala-xml-apis","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/","title":{"rendered":"Smooth Scala XML APIs"},"content":{"rendered":"<p>As an alternative to using <a href=\"http:\/\/jaxb.java.net\/\" title=\"JAXB\">JAXB<\/a> for reading input, we thought we&#8217;d try simply wrapping an XML document in a class structure. XML is a first class citizen in scala, so we thought we&#8217;d have a look at what it would be. Note that we don&#8217;t expect very large XML documents (they all easily fit into memory) but the external structure is a little bit ugly.<\/p>\n<p>We inject the XML document into the class via the constructor and then using scala X-Path equivalents, we pull out the interesting fields. I have to admit it&#8217;s better than dealing with XML xpath in the java world, and the code is pretty simple. <\/p>\n<p>I&#8217;ve written an equivalent to give you an idea of what we evolved from.<\/p>\n<p>Given we have an XML document that looks like this (domain made up here)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">&amp;lt;profile&amp;gt;\r\n  &amp;lt;names&amp;gt;\r\n     &amp;lt;defaultName&amp;gt;Harry Potter&amp;lt;\/defaultName&amp;gt;\r\n     &amp;lt;alternativeNames&amp;gt;\r\n        &amp;lt;name&amp;gt;Harry&amp;lt;\/name&amp;gt;\r\n        &amp;lt;name&amp;gt;Mr Potter&amp;lt;\/name&amp;gt;\r\n     &amp;lt;\/alternativeNames&amp;gt;\r\n  &amp;lt;\/names&amp;gt;\r\n  &amp;lt;contact&amp;gt;\r\n     &amp;lt;email&amp;gt;harry.potter@hogwarts.com&amp;lt;\/email&amp;gt;\r\n     &amp;lt;website&amp;gt;http:\/\/hogwarts.com&amp;lt;\/website&amp;gt;\r\n     &amp;lt;phone&amp;gt;http:\/\/hogwarts.com&amp;lt;\/phone&amp;gt;     \r\n  &amp;lt;\/contact&amp;gt;\r\n&amp;lt;\/profile&amp;gt;<\/pre>\n<p>We ended up with a class that looks like this<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">import xml.Node\r\nclass Profile (xml: Node) {\r\n  def name = {\r\n    (xml \\ &amp;quot;names&amp;quot; \\ &amp;quot;defaultName&amp;quot;).text\r\n  }\r\n\r\n  def email = {\r\n    (xml \\ &amp;quot;contact&amp;quot; \\ &amp;quot;email&amp;quot;).text\r\n  }\r\n\r\n  def website = {\r\n    (xml \\ &amp;quot;contact&amp;quot; \\ &amp;quot;website&amp;quot;).text\r\n  }\r\n\r\n  def phone = {\r\n    (xml \\ &amp;quot;contact&amp;quot; \\ &amp;quot;phone&amp;quot;).text\r\n  }\r\n}<\/pre>\n<p>We can apply a couple of small refactorings here. Simple one line expressions do not need curly braces. The class now becomes:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">import xml.Node\r\nclass Profile (xml: Node) {\r\n  def name =  (xml \\ &amp;quot;names&amp;quot; \\ &amp;quot;defaultName&amp;quot;).text\r\n  def email = (xml \\ &amp;quot;contact&amp;quot; \\ &amp;quot;email&amp;quot;).text\r\n  def website = (xml \\ &amp;quot;contact&amp;quot; \\ &amp;quot;website&amp;quot;).text\r\n  def phone = (xml \\ &amp;quot;contact&amp;quot; \\ &amp;quot;phone&amp;quot;).text\r\n}<\/pre>\n<p>Maybe we also wanted to remove a little bit of duplication in the xpath. Though this increases the size of the class a bit: <\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">import xml.Node\r\nclass Profile (xml: Node) {\r\n  def name =  (xml \\ &amp;quot;names&amp;quot; \\ &amp;quot;defaultName&amp;quot;).text\r\n  def email = (contact \\ &amp;quot;email&amp;quot;).text\r\n  def website = (contact \\ &amp;quot;website&amp;quot;).text\r\n  def phone = (contact \\ &amp;quot;phone&amp;quot;).text\r\n\r\n  private def contact = (xml \\ &amp;quot;contact&amp;quot;)\r\n}<\/pre>\n<p>Since our XML structure will now change, we also had some feedback that we could use the lazy val option to cache the results. The code now looks like this:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">import xml.Node\r\nclass Profile (xml: Node) {\r\n  lazy val name =  (xml \\ &amp;quot;names&amp;quot; \\ &amp;quot;defaultName&amp;quot;).text\r\n  lazy val email = (contact \\ &amp;quot;email&amp;quot;).text\r\n  lazy val website = (contact \\ &amp;quot;website&amp;quot;).text\r\n  lazy val phone = (contact \\ &amp;quot;phone&amp;quot;).text\r\n\r\n  lazy val contact = (xml \\ &amp;quot;contact&amp;quot;)\r\n}<\/pre>\n<p>I&#8217;m not so sure if the last two refactorings give much more than the second refactoring, but it certainly helped me learn a a bit more scala. I think we had some pretty good wins by dealing with XML the scala way. Our tests are very readable because you don&#8217;t have weird string concatenation or sucking in XML from a file to test it, and I think the code is quite readable. I would have preferred closer XPath-y syntax, but I guess learning the scala XML syntax for traversing XML isn&#8217;t too bad. The other good thing about this is that you don&#8217;t have to worry about null or non existant nodes &#8211; you simply get back an empty string. Pretty decent default behaviour for at least our use case. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>As an alternative to using JAXB for reading input, we thought we&#8217;d try simply wrapping an XML document in a class structure. XML is a first class citizen in scala, so we thought we&#8217;d have a look at what it would be. Note that we don&#8217;t expect very large XML documents (they all easily fit [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,55],"tags":[],"class_list":["post-1370","post","type-post","status-publish","format-standard","hentry","category-development","category-scala","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Smooth Scala XML APIs - patkua@work<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Smooth Scala XML APIs - patkua@work\" \/>\n<meta property=\"og:description\" content=\"As an alternative to using JAXB for reading input, we thought we&#8217;d try simply wrapping an XML document in a class structure. XML is a first class citizen in scala, so we thought we&#8217;d have a look at what it would be. Note that we don&#8217;t expect very large XML documents (they all easily fit [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-05T17:15:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-18T14:08:09+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"Smooth Scala XML APIs\",\"datePublished\":\"2011-10-05T17:15:05+00:00\",\"dateModified\":\"2019-07-18T14:08:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/\"},\"wordCount\":746,\"commentCount\":2,\"articleSection\":[\"Development\",\"Scala\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/\",\"name\":\"Smooth Scala XML APIs - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2011-10-05T17:15:05+00:00\",\"dateModified\":\"2019-07-18T14:08:09+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/10\\\/smooth-scala-xml-apis\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Smooth Scala XML APIs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Smooth Scala XML APIs - patkua@work","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/","og_locale":"en_US","og_type":"article","og_title":"Smooth Scala XML APIs - patkua@work","og_description":"As an alternative to using JAXB for reading input, we thought we&#8217;d try simply wrapping an XML document in a class structure. XML is a first class citizen in scala, so we thought we&#8217;d have a look at what it would be. Note that we don&#8217;t expect very large XML documents (they all easily fit [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/","og_site_name":"patkua@work","article_published_time":"2011-10-05T17:15:05+00:00","article_modified_time":"2019-07-18T14:08:09+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"Smooth Scala XML APIs","datePublished":"2011-10-05T17:15:05+00:00","dateModified":"2019-07-18T14:08:09+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/"},"wordCount":746,"commentCount":2,"articleSection":["Development","Scala"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/","url":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/","name":"Smooth Scala XML APIs - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2011-10-05T17:15:05+00:00","dateModified":"2019-07-18T14:08:09+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2011\/10\/smooth-scala-xml-apis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"Smooth Scala XML APIs"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=1370"}],"version-history":[{"count":4,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1370\/revisions"}],"predecessor-version":[{"id":3367,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1370\/revisions\/3367"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=1370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=1370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=1370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}