1

I have searched and searched and can't figure out why this code wont load an XML element into the

. I'm trying to load the XML from a file, read for a specific element and put it's data into a specific element.

HTML:

<!DOCTYPE html>
<html>
<head>
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script>
$(document).ready(function(){
   $.ajax({
    type: "GET" ,
    url: "score1.xml" ,
    dataType: "xml" ,
    success: function(xml) {
    var xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc );
    $home = $xml.find( "home" );
    $( "#home" ).text( $home.text() );
    }       
});
});
</script>
</head>
<body><p id="home"></p>
</body>
</html>

score1.xml:

<?xml version="1.0" encoding="UTF-8"?><score><home>22</home></score>

(Should also add that while I've been using PHP/HTML for years, I am a total newbie to OOP and JQuery.)

4
  • 2
    Any network or JS errors? Commented Jan 19, 2019 at 6:55
  • No JS errors, all running through XAMPP. No console errors. Commented Jan 19, 2019 at 6:55
  • Did you try putting a breakpoint inside the success callback and inspect the xml value? Commented Jan 19, 2019 at 6:57
  • @jom Not quite sure how I'd go about that? I have used several console.logs and get null and w.fn.init {} Commented Jan 19, 2019 at 7:00

1 Answer 1

2

Just remove the parseXML line and it works perfectly:

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "score1.xml",
    dataType: "xml",
    success: function(xml) {
      var $xml = $(xml);
      $home = $xml.find("home");
      $("#home").text($home.text());
    }
  });
});

I've removed this line:

var xmlDoc = $.parseXML(xml);

And now it works perfectly.

Sign up to request clarification or add additional context in comments.

3 Comments

Good grief that was simple. Thank you! Works perfectly!
No problem whatsoever! Always glad to help @ChaseCromwell
Accepted, Stack prevents accepting an answer within 10 min of asking the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.