title: Google-Apps-Script Convert XML to JSON
body: <p>I've got an XML string, like this:</p>
  
  <p><code>'&lt;ALEXA VER="0.9" URL="davidwalsh.name/" HOME="0" AID="="&gt;&lt;SD TITLE="A" FLAGS="" HOST="davidwalsh.name"&gt;&lt;TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/&gt;&lt;LINKSIN NUM="1102"/&gt;&lt;SPEED TEXT="1421" PCT="51"/&gt;&lt;/SD&gt;&lt;SD&gt;&lt;POPULARITY URL="davidwalsh.name/" TEXT="7131"/&gt;&lt;REACH RANK="5952"/&gt;&lt;RANK DELTA="-1648"/&gt;&lt;/SD&gt;&lt;/ALEXA&gt;'</code></p>
  
  <p>I'd like to convert it into JSON format:</p>
  
  <pre><code>  {
      "ALEXA":{
         "@attributes":{
            "VER":"0.9",
            "URL":"davidwalsh.name/",
            "HOME":"0",
            "AID":"="
         },
         "SD":[
            {
               "@attributes":{
                  "TITLE":"A",
                  "FLAGS":"",
                  "HOST":"davidwalsh.name"
               },
               "TITLE":{
                  "@attributes":{
                     "TEXT":"David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"
                  }
  ...
  </code></pre>
  
  <p>I've found lot's of solutions for <a href="/questions/tagged/js" class="post-tag" title="show questions tagged &#39;js&#39;" rel="tag">js</a>, but none of them worked in <a href="/questions/tagged/google-apps-script" class="post-tag" title="show questions tagged &#39;google-apps-script&#39;" rel="tag">google-apps-script</a>.
  I've also seen this question:
  <a href="https://stackoverflow.com/questions/38340056/parsing-xml-on-a-google-apps-script">Parsing XML on a Google Apps script</a>
  but it does not exactly my case: I'de like to parse any XML into JSON, not just the provided sample.
  I've found own solution (in the answer), and not sure it matches all cases.</p>