3

I apologize to ask a question relating to this topic as it seems there are many, many topics relating to the same subject. I've read through a fair amount of them but I have not been able to find the problem with my code. I have an XML file, and I'm trying to just read a child node of each attribute (if that's the right terminology?).

The XML:

<movement>
    <name>Squat</name>
    <set>
        <weight>270</weight>
        <reps>5</reps>
    </set>
</movement>

My code is just attempting to read the name attribute of each movement.

The javascript:

    $(document).ready(function()
{
    $.ajax({
        type: "GET",
        url: "/training.xml",
        dataType: "xml",
        success: parseXml
    });
});
function parseXml(xml)
{
    $(xml).find("movement").each(function()
    {
        $("#training").append($(this).find("name").text() + "<br>");
    });
}

I found this via a tutorial online and attempted to modify it just to add the name of each movement to a div on the page. Currently it is not displaying anything. I cannot see the issue. Any help would be appreciated.

Full XML file: pastebin.com/rthMWNhm

Full HTML file: pastebin.com/6m9rBjRn

Blex
  • 95
  • 1
  • 5

1 Answers1

-1

This should do the trick. The problem is that you are already at the movement node. Find only searches for children. The each iteration will iterate over every movement element.

Demo

Javascript

var parseXml = function(xml){
    var names = '';
    $(xml).find('movement name').each(function(i,name){
        names += name.innerHTML  +'<br>';
    });  
    $('#training').append(names);
};


parseXml(xml);

Updated answer to fit new xml

Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • 1
    Still no luck. In all the responses you guys just make a variable called xml and then write it in manually. I'm starting to think it is the way I'm referencing my XML file from the HTML file. Is there anything special I have to do to get the data from an external page? – Blex Apr 22 '13 at 16:13
  • Have you tried console logging the the xml from your ajax call? see what the string is? I believe that might be your issue. If your call is succesfull you should be able to just pass that string to the method above. Also check your network tab, see if you get any errors :) – Peter Rasmussen Apr 22 '13 at 16:13
  • Just alert/console log your xml as the first thing in your parseXml function. Let us know what it is :) – Peter Rasmussen Apr 22 '13 at 16:21
  • Uncaught ReferenceError: xml is not defined main.html:32 XMLHttpRequest cannot load file:///C:/Users/Alex/Desktop/Website/training.xml. Origin null is not allowed by Access-Control-Allow-Origin. main.html:1 AJAX ERROR: error, It's giving me a bunch of errors I've never seen before/a bunch of problems I've never encountered. I've used bootstrap and jQuery before, but for some reason this page cannot find the jQuery syntax I guess... – Blex Apr 22 '13 at 16:25
  • and "C:/Users/Alex/Desktop/Website/training.xml" Is the exact location of your xml file? – Peter Rasmussen Apr 22 '13 at 16:26
  • Yes. It's in the same folder as the HTML file, and in that folder is also the bootstrap folder (which contains all the bootsrap files that are all working correctly). – Blex Apr 22 '13 at 16:27
  • Didn't see the Access-control-allow part. If you are using then - Chrome does not allow you to access local files. You can run chrome with a specific parameter if this is just a html site for local use. You can find more about that here: http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-file Internet explorere will ask your for access. Run a local webserver and you will be fine no matter which browser you use :) – Peter Rasmussen Apr 22 '13 at 16:36
  • Okay so I moved the XML file to my website's webserver. Now there isn't an error, but there also isn't any console output. – Blex Apr 22 '13 at 17:42
  • Man, I'm slow. Once I added the HTML file to the webserver (instead of just testing locally again like an idiot) it found the movements just fine. – Blex Apr 22 '13 at 18:02
  • Great stuff :) Glad I could help! – Peter Rasmussen Apr 22 '13 at 18:05