Read XML with Ajax, Javascript, DOM

Asked

Viewed 95 times

1

As it is possible to read this XML using Ajax, I tried several ways, but so far unfortunately I could not succeed, the goal is to take the number of inum (810265006252830) that will be using in concatenation with another url in the project that I am developing.

<?xml version="1.0" ?>
<results>
<result inum="810265006252830">
<field name="startedat">2020-03-18T08:00:58.796</field>
<field name="duration">37</field>
<field name="agents">9283</field>
<field name="otherparties">1633425177</field>
<field name="services">7170</field>
<field name="skills">0200</field>
<field name="switchcallid">00001101551584529176</field>
<field name="udfs"/>
</result>
</results>

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Test 1</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
</head>
<body>

<h2>The XMLHttpRequest Object</h2>

<p id="demo"></p>
 
<script>
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
         myFunction(this);
     }
  };  
  xhttp.open("GET", "https://cors-anywhere.herokuapp.com/https://devxavier.000webhostapp.com/results.xml", true);
  xhttp.send();

    function myFunction(xml) {
      var x, xmlDoc, txt;
      xmlDoc = xml.responseXML;
      txt = "";
      x = xmlDoc.getElementsByTagName('result');    
      txt += x.item(0).attributes[0].nodeValue;
      document.getElementById("demo").innerHTML = "inum: " + txt; }
      </script>

</body>
</html>

Thanks in advance!

  • Dear Celio, this is incorrect txt += x[i].getElementsByTagName('inum');, this takes a LIST of elements and nay the element content, to take the content you will need to take element by element and use .text

  • William would look something like this: Function myFunction(xml) { var x, xmlDoc, txt; xmlDoc = xml.responseXML; x = xmlDoc.getelementsbytagname('result'); var txt = x[0]. getattribute('inum'); Document.getElementById("demo"). innerHTML = txt; Alert(txt.txt); }

  • Unfortunately I still could not get the value of inum, does not present any error in the console.

  • Dear Celio, it’s not txt.txt right is .text, but one thing I noticed, you used inum, which is an attribute, getelementsbytagname is to pick up by the element name and not by the attribute, will return a list, the right would be getElementsByTagName('result'); as in the example of the documentation: https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName#Example, just replace Document with the xml context.responseXML

  • 1

    @Guilherme Nascimento managed to get the value of inum but inserting this xml on a server, including the code of the post was updated with the correction. Now I’m working on another scenario which is to get the xml on localhost. Thank you so much for the man force!!!

  • For nothing, I wish you success!

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.