Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created June 28, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/61ccf69758cd6dbdc429934564864650 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/61ccf69758cd6dbdc429934564864650 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reading Data from XML Files</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="main.css" />
</head>
<body>
<header>
<h1>Reading Data from XML Files</h1>
</header>
<main>
<ul id="swords"></ul>
<ul id="houses"></ul>
<pre id="output"></pre>
</main>
<script>
//XMLHttpRequest() - has responseXML property in the response
document.addEventListener('DOMContentLoaded', ()=>{
//fetch the data as soon as the page has loaded
let url = "read.xml";
fetch(url)
.then(response=>response.text())
.then(data=>{
//console.log(data); //string
let parser = new DOMParser();
let xml = parser.parseFromString(data, "application/xml");
document.getElementById('output').textContent = data;
console.log(xml);
buildHouseList(xml);
buildSwordList(xml);
});
})
function buildHouseList(x){
let list = document.getElementById('houses');
let houses = x.getElementsByTagName('house');
for(let i=0; i<houses.length; i++){
let li = document.createElement('li');
let house = houses[i].firstChild.nodeValue;
li.textContent = house;
list.appendChild(li);
}
}
function buildSwordList(x){
let list = document.getElementById('swords');
let swords = x.getElementsByTagName('sword');
for(let i=0; i<swords.length; i++){
let li = document.createElement('li');
let swordName = swords[i].firstChild.nodeValue;
let person = swords[i].getAttribute('owner');
li.textContent = `${swordName} - ${person}`;
list.appendChild(li);
}
}
</script>
</body>
</html>
@Chiconela
Copy link

Hello.
I've been trying to load information from xml to my html for a few days now through ajax. the problem is that the codes I created don't load, so I downloaded the above file and "read.xml" , and put them in the same folder. I tried to run the code but it also doesn't work ... is there any chance of a computer problem?

kind regards
Açuceno

@prof3ssorSt3v3
Copy link
Author

The browsers tend to give pretty good error messages in the console and in the network tab of the chrome dev tools. What does it say the problem is?

@Chiconela
Copy link

Chiconela commented Aug 17, 2020 via email

@Chiconela
Copy link

Chiconela commented Aug 17, 2020 via email

@prof3ssorSt3v3
Copy link
Author

My page does not need a button. I am using the DOMContentLoaded event as the trigger to fetch the XML file with fetch.

Are you running the page over http/https? or trying to load the page as a file with file:/// ? File will not let you load anything over AJAX.

@vnashBio
Copy link

Hi how do i fetch an xml file from url but that happens to not be encoded, xml .ie?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment