{"id":7822,"date":"2015-03-13T14:37:51","date_gmt":"2015-03-13T14:37:51","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/converting-xml-files-to-yaml-or-pson\/"},"modified":"2016-07-07T13:16:30","modified_gmt":"2016-07-07T13:16:30","slug":"converting-xml-files-to-yaml-or-pson","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/converting-xml-files-to-yaml-or-pson\/","title":{"rendered":"Converting XML files to YAML or PSON"},"content":{"rendered":"<p class=\"MsoNormal\">\nThe other day, I needed to convert a whole stack of XML files to YAML.\u00a0 Actually , I would have settled for a conversion to JSON, but for some reason, the built-in cmdlet wouldn&#8217;t do it. I was trying to figure out a way of doing the YAML conversion when I suddenly remembered I&#8217;d actually already published a way of doing it, in &#8216;<a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/getting-data-into-and-out-of--powershell-objects\/\">Getting Data Into and Out of PowerShell Objects&#8217;<\/a>. With relief, I got the routine out and tried it. It didn&#8217;t work, because I&#8217;d chosen to display an XML value as an <b>innerXML<\/b> string. This is the behaviour you might want, but not in this case.<\/p>\n<p class=\"MsoNormal\">There are two different ways of rendering XML values. You can render the object represented by the XML or you can do it just as an InnerXML string. The former method allows us to convert XML files directly into YAML or PSON.\u00a0 Useful? \u00a0Sure. It is easier to demo than explain. First\u00a0 rendering it as an object&#8230;<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true\">  \r\n$xml= [Xml] @' \r\n   &lt;emp id=\"salary='60000'&gt;\"&gt; \r\n   &lt;name&gt; \r\n    &lt;first&gt;William&lt;\/first&gt; \r\n    &lt;la st&gt;Murphy&lt;\/last&gt; \r\n   &lt;\/name&gt; \r\n   &lt;spouse&gt; \r\n    &lt;name&gt; \r\n     &lt;first&gt;Cecilia Bertha Matilda&lt;\/first&gt; \r\n     &lt;last&gt;Murphy&lt;\/last&gt; \r\n    &lt;\/name&gt; \r\n   &lt;\/spouse&gt; \r\n   &lt;dept id=\"K55'&gt;Finance&lt;\/dept&gt;\"&gt; \r\n  &lt;\/emp&gt; \r\n  '@ \r\n   ConvertTo-YAML $xml \r\n<\/pre>\n<p>&#8230;giving &#8230;<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true\">  \r\n--- \r\n  \u00a0 \r\n  \u00a0emp:\u00a0\u00a0 \r\n  \u00a0\u00a0\u00a0dept:\u00a0\u00a0 \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0#text:\u00a0\u00a0\u00a0'Finance' \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0id:\u00a0\u00a0\u00a0'K55' \r\n  \u00a0\u00a0\u00a0id:\u00a0\u00a0\u00a0'12345' \r\n  \u00a0\u00a0\u00a0name:\u00a0\u00a0 \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0first:\u00a0\u00a0\u00a0'William' \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0last:\u00a0\u00a0\u00a0'Murphy' \r\n  \u00a0\u00a0\u00a0salary:\u00a0\u00a0\u00a0'60000' \r\n  \u00a0\u00a0\u00a0spouse:\u00a0\u00a0 \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0name:\u00a0\u00a0 \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0first:\u00a0\u00a0\u00a0'Cecilia Bertha Matilda' \r\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0last:\u00a0\u00a0\u00a0'Murphy' \r\n  \u00a0 \r\n<\/pre>\n<p class=\"MsoNormal\">and the other way, simply giving it as an XML fragment, (note the parameter if you want that behaviour)\u00a0 is.<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true\">  \r\n$xml=\u00a0[Xml]\u00a0@' \r\n  \u00a0&lt;emp id=\"salary='60000'&gt;\"&gt; \r\n  \u00a0&lt;name&gt; \r\n  \u00a0\u00a0&lt;first&gt;William&lt;\/first&gt; \r\n  \u00a0\u00a0&lt;last&gt;Murphy&lt;\/last&gt; \r\n  \u00a0&lt;\/name&gt; \r\n  \u00a0&lt;spouse&gt; \r\n  \u00a0\u00a0&lt;name&gt; \r\n  \u00a0\u00a0\u00a0&lt;first&gt;Cecilia Bertha Matilda&lt;\/first&gt; \r\n  \u00a0\u00a0\u00a0&lt;last&gt;Murphy&lt;\/last&gt; \r\n  \u00a0\u00a0&lt;\/name&gt; \r\n  \u00a0&lt;\/spouse&gt; \r\n  \u00a0&lt;dept id=\"K55'&gt;Finance&lt;\/dept&gt;\"&gt; \r\n  &lt;\/emp&gt; \r\n  '@ \r\n  \u00a0ConvertTo-YAML\u00a0$xml\u00a0-XMLAsInnerXML\u00a01 \r\n<\/pre>\n<p class=\"MsoNormal\">&#8230; giving &#8230;<\/p>\n<pre class=\"theme:powershell-ise lang:ps decode:true\">  \r\n--- \r\n  | \r\n  &lt;emp id=\"12345\" salary=\"60000\"&gt;&lt;name&gt;&lt;first&gt;William&lt;\/first&gt;&lt;last&gt;Murphy&lt;\/last&gt;&lt;\/name&gt;&lt;spouse&gt;&lt;name&gt;&lt;first&gt;Cecilia Bertha matilda&lt;\/first&gt;&lt;last&gt;Murphy&lt;\/last&gt;&lt;\/name&gt;&lt;\/spouse&gt;&lt;dept id=\"K55\"&gt;Finance&lt;\/dept&gt;&lt;\/emp&gt; \r\n  \r\n<\/pre>\n<\/p>\n<p class=\"MsoNormal\">It turned out that it was just as easy to handle the XML as an object as an XML document, and I&#8217;d already developed a way of doing a PowerShell object. After modifying the routine, and updating the article, I tried again. Nice. It worked a treat, and the YAML parser gulped it all in with no complaint. It wasn&#8217;t quick by any means with multi-megabyte XML files, but who cares when there are good videos to be watched? The PowerShell scripts are with the article <a href=\"https:\/\/www.simple-talk.com\/sysadmin\/powershell\/getting-data-into-and-out-of--powershell-objects\/\"> here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day, I needed to convert a whole stack of XML files to YAML.\u00a0 Actually , I would have settled for a conversion to JSON, but for some reason, the built-in cmdlet wouldn&#8217;t do it. I was trying to figure out a way of doing the YAML conversion when I suddenly remembered I&#8217;d actually&#8230;&hellip;<\/p>\n","protected":false},"author":154613,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-7822","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/154613"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=7822"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7822\/revisions"}],"predecessor-version":[{"id":22905,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7822\/revisions\/22905"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=7822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=7822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=7822"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=7822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}