title: RegEx Convert XML to JSON
body: <p>This is a continuation of a previous question:
  <a href="https://stackoverflow.com/questions/15653460/i-need-to-convert-xml-to-json-in-javascript-on-parse-coms-cloud-code">I need to convert XML to Json in JavaScript on Parse.com&#39;s Cloud Code</a></p>
  
  <p><strong>Please don't down vote this because you don't believe RegEx is the right choice for this.  It's what I have to work with.</strong>  If you have another idea of a way to do this, please let me know.  But it must run on Parse.com's Cloud Code.  </p>
  
  <p>Original XML:</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;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;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;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>RegEx Code:</p>
  
  <pre><code>var regex = /(&lt;\w+[^&lt;]*?)\s+([\w-]+)="([^"]+)"&gt;/;
              while(xml.match(regex)) xml = xml.replace(regex, '&lt;$2&gt;$3&lt;/$2&gt;$1&gt;'); // For attributes
  
              xml = xml.replace(/\s/g, ' ').  // Finds all the white space converts to single space
                      replace(/&lt; *\?[^&gt;]*?\? *&gt;/g, ''). //Finds the XML header and removes it
                      replace(/&lt; *!--[^&gt;]*?-- *&gt;/g, ''). //Finds and removes all comments
                      replace(/&lt; *(\/?) *(\w[\w-]+\b):(\w[\w-]+\b)/g, '&lt;$1$2_$3').
                      replace(/&lt; *(\w[\w-]+\b)([^&gt;]*?)\/ *&gt;/g, '&lt; $1$2&gt;').
                      replace(/(\w[\w-]+\b):(\w[\w-]+\b) *= *"([^&gt;]*?)"/g, '$1_$2="$3"').
                      replace(/&lt; *(\w[\w-]+\b)((?: *\w[\w-]+ *= *" *[^"]*?")+ *)&gt;( *[^&lt; ]*?\b.*?)&lt; *\/ *\1 *&gt;/g, '&lt; $1$2 value="$3"&gt;').
                      //replace(/ *(\w[\w-]+\b) *= *"([^&gt;]*?)" */g, '&lt; $1&gt;$2').
                      replace(/&lt; *(\w[\w-]+\b) *&lt;/g, '&lt;$1&gt;&lt; ').
                      replace(/&gt; *&gt;/g, '&gt;').
                      //replace(/&lt; *\/ *(\w[\w-]+\b) *&gt; *&lt; *\1 *&gt;/g, '').  // breaks the output?
                      replace(/"/g, '\\"').
                      replace(/&lt; *(\w[\w-]+\b) *&gt;([^&lt;&gt;]*?)&lt; *\/ *\1 *&gt;/g, '"$1":"$2",').
                      replace(/&lt; *(\w[\w-]+\b) *&gt;([^&lt;&gt;]*?)&lt; *\/ *\1 *&gt;/g, '"$1":{$2},').
                      replace(/&lt; *(\w[\w-]+\b) *&gt;(?=.*?&lt; \/\1\},\{)/g, '"$1":[{').
                      split(/\},\{/).
                      reverse().
                      join('},{').
                      replace(/&lt; *\/ *(\w[\w-]+\b) *&gt;(?=.*?"\1":\[\{)/g, '}],').
                      split(/\},\{/).
                      reverse().
                      join('},{').
                      replace(/&lt; \/(\w[\w-]+\b)\},\{\1&gt;/g, '},{').
                      replace(/&lt; *(\w[\w-]+\b)[^&gt;]*?&gt;/g, '"$1":{').
                      replace(/&lt; *\/ *\w[\w-]+ *&gt;/g,'},').
                      replace(/\} *,(?= *(\}|\]))/g, '}').
                      replace(/] *,(?= *(\}|\]))/g, ']').
                      replace(/" *,(?= *(\}|\]))/g, '"').
                      replace(/ *, *$/g, '');
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>"api": {
      "page-number": "1",
      "records-returned": "1",
      "total-matched": "1618",
      "products": {
          "product": {
              "ad-id": "1234",
              "supplier-name": "Window World",
              "supplier-category": "3703703",
              "buy-url": "http://website.com",
              "currency": "USD",
              "description": "Window",
              "image-url": "http://website.com/windowa/80x80.jpg",
              "in-stock": "yes",
              "manufacturer-name": "Window World",
              "name": "Half Pain Glass",
              "price": "31.95",
              "retail-price": "87.60",
              "sale-price": "29.95",
              "sku": "5938",
              "upc": ""
          },
          "product": {
              "ad-id": "1234",
              "supplier-name": "Window World",
              "supplier-category": "3703703",
              "buy-url": "http://website.com",
              "currency": "USD",
              "description": "Window",
              "image-url": "http://website.com/windowa/80x80.jpg",
              "in-stock": "yes",
              "manufacturer-name": "Window World",
              "name": "Half Pain Glass",
              "price": "31.95",
              "retail-price": "87.60",
              "sale-price": "29.95",
              "sku": "5938",
              "upc": ""
          },
          "product": {
              "ad-id": "1234",
              "supplier-name": "Window World",
              "supplier-category": "3703703",
              "buy-url": "http://website.com",
              "currency": "USD",
              "description": "Window",
              "image-url": "http://website.com/windowa/80x80.jpg",
              "in-stock": "yes",
              "manufacturer-name": "Window World",
              "name": "Half Pain Glass",
              "price": "31.95",
              "retail-price": "87.60",
              "sale-price": "29.95",
              "sku": "5938",
              "upc": ""
          },
          "product": {
              "ad-id": "1234",
              "supplier-name": "Window World",
              "supplier-category": "3703703",
              "buy-url": "http://website.com",
              "currency": "USD",
              "description": "Window",
              "image-url": "http://website.com/windowa/80x80.jpg",
              "in-stock": "yes",
              "manufacturer-name": "Window World",
              "name": "Half Pain Glass",
              "price": "31.95",
              "retail-price": "87.60",
              "sale-price": "29.95",
              "sku": "5938",
              "upc": ""
          }
      }
  }
  </code></pre>
  
  <p>The last issue I'm having with this (that I know of) is this doesn't make repeating items a JSON array.  Any ideas on how to solve this? </p>