0

So I have this XML

<food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>
    Thick slices made from our homemade sourdough bread
    </description>
    <calories>600</calories>
</food>

I want to get just the name of the food. How can I do that? I've already tried with this code:

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName('title')[0];
    var y = x.childNodes[0];
    document.getElementById("demo").innerHTML = y.nodeValue; 
}
</script>

https://jsfiddle.net/jLa6kanr/2/

AleX6Ndru
  • 1
  • 1

1 Answers1

-1
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "http://localhost/foldername/book.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName('name')[0];
    var y = x.childNodes[0];
    document.getElementById("demo").innerHTML = y.nodeValue; 
}
</script>
yjs
  • 692
  • 3
  • 11
  • It doesn't work. Look at my fiddle link from my question. – AleX6Ndru Apr 12 '18 at 12:45
  • It is cross browser access issue....Please check the console and see the error – yjs Apr 12 '18 at 12:50
  • Failed to load https://steamcommunity.com/profiles/76561198051219432/?xml=1: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access. – yjs Apr 12 '18 at 12:50
  • Your xml and html file should be served under same domain – yjs Apr 12 '18 at 12:53
  • So I can't get the XML from Steam to my website? – AleX6Ndru Apr 12 '18 at 13:00