title: I need to convert XML to Json in JavaScript on Parse.com's Cloud Code
body: <p>PLEASE DO NOT COMMENT SAYING YOU CANNOT PARSE XML WITH REGEX, IT CAN BE DONE IT'S JUST NOT THE BEST WAY. AND PLEASE DON'T NEGATIVE THIS QUESTION FOR NO REASON.</p>
  
  <p>On Parse.com's cloud code you currently cannot convert XML to json without major coding effort.  I found the following code at: <a href="http://killzonekid.com/worlds-smallest-fastest-xml-to-json-javascript-converter/" rel="nofollow">http://killzonekid.com/worlds-smallest-fastest-xml-to-json-javascript-converter/</a></p>
  
  <pre><code>xml = xml.replace(/\s/g, ' ').replace(/&lt; *\?[^&gt;]*?\? *&gt;/g, '').replace(/&lt; *!--[^&gt;]*?-- *&gt;/g, '').replace(/&lt; *(\/?) *(\w+\b):(\w+\b)/g, '&lt;$1$2_$3').replace(/&lt; *(\w+\b)([^&gt;]*?)\/ *&gt;/g, '&lt; $1$2&gt;').replace(/(\w+\b):(\w+\b) *= *"([^&gt;]*?)"/g, '$1_$2="$3"').replace(/&lt; *(\w+\b)((?: *\w+ *= *" *[^"]*?")+ *)&gt;( *[^&lt; ]*?\b.*?)&lt; *\/ *\1 *&gt;/g, '&lt; $1$2 value="$3"&gt;').replace(/ *(\w+\b) *= *"([^&gt;]*?)" */g, '&lt; $1&gt;$2').replace(/&lt; *(\w+\b) *&lt;/g, '&lt;$1&gt;&lt; ').replace(/&gt; *&gt;/g, '&gt;').replace(/&lt; *\/ *(\w+\b) *&gt; *&lt; *\1 *&gt;/g, '').replace(/"/g, '\\"').replace(/&lt; *(\w+\b) *&gt;([^&lt;&gt;]*?)&lt; *\/ *\1 *&gt;/g, '"$1":"$2",').replace(/&lt; *(\w+\b) *&gt;([^&lt;&gt;]*?)&lt; *\/ *\1 *&gt;/g, '"$1":{$2},').replace(/&lt; *(\w+\b) *&gt;(?=.*?&lt; \/\1\},\{)/g, '"$1":[{').split(/\},\{/).reverse().join('},{').replace(/&lt; *\/ *(\w+\b) *&gt;(?=.*?"\1":\[\{)/g, '}],').split(/\},\{/).reverse().join('},{').replace(/&lt; \/(\w+\b)\},\{\1&gt;/g, '},{').replace(/&lt; *(\w+\b)[^&gt;]*?&gt;/g, '"$1":{').replace(/&lt; *\/ *\w+ *&gt;/g,'},').replace(/\} *,(?= *(\}|\]))/g, '}').replace(/] *,(?= *(\}|\]))/g, ']').replace(/" *,(?= *(\}|\]))/g, '"').replace(/ *, *$/g, '');
  </code></pre>
  
  <p>It actually does quite a good job of converting XML to json.</p>
  
  <p>There are a few querks with the code. 
  1. It messes up the attributes.</p>
  
  <ol>
  <li>It doesn't like names with hyphens in them.
  To fix the hyphens I changed all the \w+ to \w[\w'-]  Is this the best way? </li>
  </ol>
  
  <p>Here is an example XML document</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;api&gt;
      &lt;products total-matched="1618" records-returned="1" page-number="1"&gt;
          &lt;product&gt;
              &lt;ad-id&gt;1234&lt;/ad-id&gt;
              &lt;supplier-name&gt;Window World&lt;/supplier-name&gt;
              &lt;supplier-category&gt;3703703&lt;/supplier-category&gt;
              &lt;buy-url&gt;http://website.com&lt;/buy-url&gt;
              &lt;currency&gt;USD&lt;/currency&gt;
              &lt;description&gt;Window&lt;/description&gt;
              &lt;image-url&gt;http://website.com/windowa/80x80.jpg&lt;/image-url&gt;
              &lt;in-stock&gt;yes&lt;/in-stock&gt;
              &lt;manufacturer-name&gt;Window World&lt;/manufacturer-name&gt;
              &lt;name&gt;Half Pain Glass&lt;/name&gt;
              &lt;price&gt;31.95&lt;/price&gt;
              &lt;retail-price&gt;87.60&lt;/retail-price&gt;
              &lt;sale-price&gt;29.95&lt;/sale-price&gt;
              &lt;sku&gt;5938&lt;/sku&gt;
              &lt;upc&gt;&lt;/upc&gt;
          &lt;/product&gt;
      &lt;/products&gt;
  &lt;/api&gt;
  </code></pre>
  
  <p>Example output:</p>
  
  <pre><code>{
      "api": {
          "products": {
              "total-matched": {
                  1618 "records-returned": {
                      1 "page-number": {
                          1 &gt;
                              "product": {
                              "adid": "1234",
                              "suppliername": "Window World",
                              "suppliercategory": "3703703",
                              "buyurl": "http://website.com",
                              "currency": "USD",
                              "description": "Window",
                              "imageurl": "http://website.com/windowa/80x80.jpg",
                              "instock": "yes",
                              "manufacturername": "Window World",
                              "name": "Half Pain Glass",
                              "price": "31.95",
                              "retailprice": "87.60",
                              "saleprice": "29.95",
                              "sku": "5938",
                              "upc": ""
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>