{"id":492,"date":"2010-05-29T11:49:53","date_gmt":"2010-05-29T10:49:53","guid":{"rendered":"http:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/"},"modified":"2010-05-29T11:49:53","modified_gmt":"2010-05-29T10:49:53","slug":"how-to-generate-an-xml-document-with-xmllibxml","status":"publish","type":"post","link":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/","title":{"rendered":"How to generate an XML document with XML::LibXML"},"content":{"rendered":"<p>For the &quot;note to self&quot; series, or&#8230; if I ever need to do this again, I will find this here, here&#39;s how to generate an XML document with <a href=\"http:\/\/search.cpan.org\/dist\/XML-LibXML\" rel=\"nofollow\">XML::LibXML<\/a>. I usually use <a href=\"http:\/\/search.cpan.org\/dist\/XML-Simple\" rel=\"nofollow\">XML::Simple<\/a> or <a href=\"http:\/\/search.cpan.org\/dist\/XML-TreePP\" rel=\"nofollow\">XML::TreePP<\/a>, but this script was already requiring XML::LibXML to parse some XML fragments, so I thought I would use the same module. It wasn&#39;t really immediate&#8230;<\/p>\n<pre>\r\n#!\/usr\/bin\/env perl\r\n\r\n#\r\n# Create a simple XML document\r\n#\r\n\r\nuse strict;\r\nuse warnings;\r\nuse XML::LibXML;\r\n\r\nmy $doc = XML:: LibXML:: Document-&gt;new(&#39;1.0&#39;, &#39;utf-8&#39;);\r\n\r\nmy $root = $doc-&gt;createElement(&quot;my-root-element&quot;);\r\n$root-&gt;setAttribute(&#39;some-attr&#39;=&gt; &#39;some-value&#39;);\r\n\r\nmy %tags = (\r\n    color =&gt; &#39;blue&#39;,\r\n    metal =&gt; &#39;steel&#39;,\r\n);\r\n\r\nfor my $name (keys %tags) {\r\n    my $tag = $doc-&gt;createElement($name);\r\n    my $value = $tags{$name};\r\n    $tag-&gt;appendTextNode($value);\r\n    $root-&gt;appendChild($tag);\r\n}\r\n\r\n$doc-&gt;setDocumentElement($root);\r\nprint $doc-&gt;toString();\r\n<\/pre>\n<p>and this outputs:<\/p>\n<pre>\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;my-root-element some-attr=&quot;some-value&quot;&gt;\r\n    &lt;color&gt;blue&lt;\/color&gt;\r\n    &lt;metal&gt;steel&lt;\/metal&gt;\r\n&lt;\/my-root-element&gt;\r\n<\/pre>\n<p>And a copy is also archived at <a href=\"http:\/\/stackoverflow.com\/questions\/154762\/how-can-i-create-xml-from-perl\/2934794#2934794\" rel=\"nofollow\">stackoverflow<\/a> :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the &quot;note to self&quot; series, or&#8230; if I ever need to do this again, I will find this here, here&#39;s how to generate an XML document with XML::LibXML. I usually use XML::Simple or XML::TreePP, but this script was already requiring XML::LibXML to parse some XML fragments, so I thought I would use the same [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[50,433,432],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to generate an XML document with XML::LibXML - Random hacking<\/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:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to generate an XML document with XML::LibXML - Random hacking\" \/>\n<meta property=\"og:description\" content=\"For the &quot;note to self&quot; series, or&#8230; if I ever need to do this again, I will find this here, here&#039;s how to generate an XML document with XML::LibXML. I usually use XML::Simple or XML::TreePP, but this script was already requiring XML::LibXML to parse some XML fragments, so I thought I would use the same [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\" \/>\n<meta property=\"og:site_name\" content=\"Random hacking\" \/>\n<meta property=\"article:published_time\" content=\"2010-05-29T10:49:53+00:00\" \/>\n<meta name=\"author\" content=\"cosimo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cosimo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\"},\"author\":{\"name\":\"cosimo\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\"},\"headline\":\"How to generate an XML document with XML::LibXML\",\"datePublished\":\"2010-05-29T10:49:53+00:00\",\"dateModified\":\"2010-05-29T10:49:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\"},\"wordCount\":88,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\"},\"keywords\":[\"perl\",\"xml\",\"XML::LibXML\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\",\"url\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\",\"name\":\"How to generate an XML document with XML::LibXML - Random hacking\",\"isPartOf\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#website\"},\"datePublished\":\"2010-05-29T10:49:53+00:00\",\"dateModified\":\"2010-05-29T10:49:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.streppone.it\/cosimo\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to generate an XML document with XML::LibXML\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#website\",\"url\":\"https:\/\/www.streppone.it\/cosimo\/blog\/\",\"name\":\"Random hacking\",\"description\":\"Assume nothing. Code defensively. Keep it simple, stupid!\",\"publisher\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.streppone.it\/cosimo\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\",\"name\":\"cosimo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g\",\"caption\":\"cosimo\"},\"logo\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.streppone.it\/cosimo\/blog\/author\/cosimo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to generate an XML document with XML::LibXML - Random hacking","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:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/","og_locale":"en_US","og_type":"article","og_title":"How to generate an XML document with XML::LibXML - Random hacking","og_description":"For the &quot;note to self&quot; series, or&#8230; if I ever need to do this again, I will find this here, here&#39;s how to generate an XML document with XML::LibXML. I usually use XML::Simple or XML::TreePP, but this script was already requiring XML::LibXML to parse some XML fragments, so I thought I would use the same [&hellip;]","og_url":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/","og_site_name":"Random hacking","article_published_time":"2010-05-29T10:49:53+00:00","author":"cosimo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"cosimo","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#article","isPartOf":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/"},"author":{"name":"cosimo","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1"},"headline":"How to generate an XML document with XML::LibXML","datePublished":"2010-05-29T10:49:53+00:00","dateModified":"2010-05-29T10:49:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/"},"wordCount":88,"commentCount":0,"publisher":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1"},"keywords":["perl","xml","XML::LibXML"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/","url":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/","name":"How to generate an XML document with XML::LibXML - Random hacking","isPartOf":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#website"},"datePublished":"2010-05-29T10:49:53+00:00","dateModified":"2010-05-29T10:49:53+00:00","breadcrumb":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/how-to-generate-an-xml-document-with-xmllibxml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.streppone.it\/cosimo\/blog\/"},{"@type":"ListItem","position":2,"name":"How to generate an XML document with XML::LibXML"}]},{"@type":"WebSite","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#website","url":"https:\/\/www.streppone.it\/cosimo\/blog\/","name":"Random hacking","description":"Assume nothing. Code defensively. Keep it simple, stupid!","publisher":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.streppone.it\/cosimo\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1","name":"cosimo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g","caption":"cosimo"},"logo":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/www.streppone.it\/cosimo\/blog\/author\/cosimo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/posts\/492"}],"collection":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":0,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}