title: Get a XML by using AJAX then parse it into JSON (without server)
body: <p>I asking for help to a little code that I'm working on.
  But I'm not good with AJAX and Jquery... So here's my topic : I wanted to get a XML with a external XML file (here text.xml) and parse it into a JSON object with the the code of David Walsh (source here : <a href="https://davidwalsh.name/convert-xml-json" rel="nofollow">https://davidwalsh.name/convert-xml-json</a> ).
  Then someone said the function can only be in the "success" part that's why I'm doig all my function on it.
  So my goal is to have my XML into JSON object that i can treat it easier, I find some forum topic that answer to my question but my code aren't working and I don't why... If you can help me thank you.
  There is my code : </p>
  
  <pre><code> &lt;HTML&gt;
  
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
      &lt;title&gt;testSelection&lt;/title&gt;
      &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;body&gt;
          &lt;div id="Div_XML"&gt;&lt;/div&gt;
      &lt;/body&gt;
  
      &lt;script&gt;
          var xml = $.parseXML('&lt;bds&gt; &lt;serie1 id="1"&gt; &lt;desc&gt; &lt;num&gt; 2 &lt;/num&gt;&lt;title&gt;we are on 1 &lt;/title&gt;&lt;commen&gt;HI &lt;/commen&gt;&lt;URL&gt;Idk&lt;/URL&gt;&lt;/desc&gt;&lt;desc&gt;&lt;num&gt; 1 &lt;/num&gt;&lt;title&gt;we are on  2 &lt;/title&gt;&lt;commen&gt;Hello world &lt;/commen&gt;&lt;URL&gt;Baptiste&lt;/URL&gt;&lt;/desc&gt;&lt;/serie1&gt;&lt;/bds&gt;')
          var results = new Array;
          var obj = {};
  
  
          $(document).ready(
                  function() {
                      $.ajax({
                              type: "GET",
                              url: "text.xml",
                              dataType: "xml",
                              success: function(xml) {
  
                                  var commen = $(this).find('commen').text();
                                  var title = $(this).find('title').text();
                                  var URL = $(this).find('URL').text();
                                  var num = $(this).find('num').text();
  
                                  $(xml).find('bds').each(
  
                                      function() {
                                          results.push({
                                              id: $(this).attr('id'),
                                              num: num,
                                              commen: commen,
                                              title: title,
                                              URL: URL});
                                          });
                                          // Changes XML to JSON
  
  
                                              // Create the return object
                                           function parseX(results){    
  
                                              if (results.nodeType == 1) { // element
                                                  // do attributes
                                                  if (results.attributes.length &gt; 0) {
                                                      obj["@attributes"] = {};
                                                      for (var j = 0; j &lt; results.attributes.length; j++) {
                                                          var attribute = results.attributes.item(j);
                                                          obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
                                                      }
                                                  }
                                              } else if (results.nodeType == 3) { // text
                                                  obj = results.nodeValue;
                                              }
  
                                              // do children
                                              if (results.hasChildNodes()) {
                                                  for (var i = 0; i &lt; results.childNodes.length; i++) {
                                                      var item = results.childNodes.item(i);
                                                      var nodeName = item.nodeName;
                                                      if (typeof(obj[nodeName]) == "undefined") {
                                                          obj[nodeName] = xmlToJson(item);
                                                      } else {
                                                          if (typeof(obj[nodeName].push) == "undefined") {
                                                              var old = obj[nodeName];
                                                              obj[nodeName] = [];
                                                              obj[nodeName].push(old);
  
                                                          }
                                                          obj[nodeName].push(xmlToJson(item));
                                                      }
                                                  }
                                              }
  
  
  
                                          // Assuming xmlDoc is the XML DOM Document
                                          var jsonText = JSON.stringify(xmlToJson(results));
                                          alert(jsonText);
                                          }
                                          }
                                      )
                                  };
                              }
                          }
                      )
      &lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/HTML&gt;
  </code></pre>
  
  <p>So my idea was to push in an array all of my xml data then parse it into json.
  So I have questions  : </p>
  
  <ul>
  <li>-Can you gave to the code of David Walsh an array ?</li>
  <li>-Can we do more than one function into the success part of AJAX ?</li>
  <li>-Can my JSON object can be split by series ? (i give the xml just after you will see serie1, serie2, serie3)</li>
  <li>-Can I compare the num (Json object) with numbers ?  ( like text.num == 1 for exemple)</li>
  </ul>
  
  <p>Sorry for my beginner questions...</p>
  
  <p>And the text.xml</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
  &lt;bds&gt;
          &lt;serie1 id="1"&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 1 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 1 &lt;/title&gt;
                          &lt;commen&gt;Hello &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 2 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 2 &lt;/title&gt;
                          &lt;commen&gt;HI &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 3 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 3 &lt;/title&gt;
                          &lt;commen&gt;How are you  &lt;/commen&gt;
                          &lt;URL&gt;Gars inconnu&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 4 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 4 &lt;/title&gt;
                          &lt;commen&gt;Idk  &lt;/commen&gt;
                          &lt;URL&gt;Raymond&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 5 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 5 &lt;/title&gt;
                          &lt;commen&gt;Thank you &lt;/commen&gt;
                          &lt;URL&gt;Eric&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 6 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 6 &lt;/title&gt;
                          &lt;commen&gt;You're welcome  &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 7 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 7 &lt;/title&gt;
                          &lt;commen&gt;Sorry &lt;/commen&gt;
                          &lt;URL&gt;CÃ©line&lt;/URL&gt;
                  &lt;/desc&gt;
          &lt;/serie1&gt;
              &lt;serie2 id="2"&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 1 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 1 &lt;/title&gt;
                          &lt;commen&gt;Hello &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 2 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 2 &lt;/title&gt;
                          &lt;commen&gt;HI &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 3 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 3 &lt;/title&gt;
                          &lt;commen&gt;How are you  &lt;/commen&gt;
                          &lt;URL&gt;Gars inconnu&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 4 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 4 &lt;/title&gt;
                          &lt;commen&gt;Idk  &lt;/commen&gt;
                          &lt;URL&gt;Raymond&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 5 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 5 &lt;/title&gt;
                          &lt;commen&gt;Thank you &lt;/commen&gt;
                          &lt;URL&gt;Eric&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 6 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 6 &lt;/title&gt;
                          &lt;commen&gt;You're welcome  &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 7 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 7 &lt;/title&gt;
                          &lt;commen&gt;Sorry &lt;/commen&gt;
                          &lt;URL&gt;CÃ©line&lt;/URL&gt;
                  &lt;/desc&gt;
          &lt;/serie2&gt;
              &lt;serie3 id="3"&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 1 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 1 &lt;/title&gt;
                          &lt;commen&gt;Hello &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 2 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 2 &lt;/title&gt;
                          &lt;commen&gt;HI &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 3 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 3 &lt;/title&gt;
                          &lt;commen&gt;How are you  &lt;/commen&gt;
                          &lt;URL&gt;Gars inconnu&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 4 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 4 &lt;/title&gt;
                          &lt;commen&gt;Idk  &lt;/commen&gt;
                          &lt;URL&gt;Raymond&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 5 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 5 &lt;/title&gt;
                          &lt;commen&gt;Thank you &lt;/commen&gt;
                          &lt;URL&gt;Eric&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 6 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 6 &lt;/title&gt;
                          &lt;commen&gt;You're welcome  &lt;/commen&gt;
                          &lt;URL&gt;Baptiste&lt;/URL&gt;
                  &lt;/desc&gt;
                  &lt;desc&gt;
                          &lt;num&gt; 7 &lt;/num&gt;
                          &lt;title&gt;nous sommes sur le lien 7 &lt;/title&gt;
                          &lt;commen&gt;Sorry &lt;/commen&gt;
                          &lt;URL&gt;CÃ©line&lt;/URL&gt;
                  &lt;/desc&gt;
          &lt;/serie3&gt;
  &lt;/bds&gt;
  </code></pre>
  
  <p>I was using the var xml to do test in local and it work but when I need to get the XML from a file I'm completely lost.
  Then I don't use JSON at the begining instead of XML beacause I want later for my project to write into it and I read it's not possible in JSON...
  By the way I can't using XmlHTTPRequest and PHP, i know that would be the  better and easier way to do it...
  Hope you have an answer to my question and thank you for your reading.</p>