Ren
Ren

Reputation: 1503

Jquery - Couldn't get the retrieved xml file to populate the DOM

I'm trying to retrieve an xml file, and display the contents on the page, but I couldn't get it working thus far. I can confirm that the file is successfully retrieved, as I get a 200 on network capture. I tried placing an alert box for $title, but it is blank.

I don't know what i'm doing wrong here. Any help would be greatly appreciated. Thanks

Below is my markup

<div id="textArea">This is a test</div>

MyScript.js

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "BooksCatalog.xml",
        dataType: "xml",
        success: function (result) {
            var xmlDoc = $.parseXML(result),
            $xml = $(xmlDoc),
            $title = $xml.find("title");
            $("#textArea").append($title.text());        
        }
    });
});

BooksCatalog.xml

<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>
      An in-depth look at creating applications
      with XML.
    </description>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>
      A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.
    </description>
  </book>
</catalog>

Upvotes: 0

Views: 117

Answers (1)

Jacob Mouka
Jacob Mouka

Reputation: 2095

I believe the problem is the call to parseXML. jQuery should already be returning a parsed result, so this step is not necessary, and likely causing the issue. Try commenting it out and simply call

$("#textArea").append($(result).find('title')); 

Upvotes: 1

Related Questions