Created
June 28, 2018 13:47
-
-
Save prof3ssorSt3v3/61ccf69758cd6dbdc429934564864650 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
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?
It says nothing... only the button is shown
A Dom, 16 de ago de 2020, 20:01, Steve Griffith <notifications@github.com>
escreveu:
… ***@***.**** commented on this gist.
------------------------------
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?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/61ccf69758cd6dbdc429934564864650#gistcomment-3419343>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOQJ46W3E5QWF6RVUD5SJQDSBANJLANCNFSM4QATQLPQ>
.
I mean, your code doesn't have onclick function, so there's no button... so
it displays a blank page... no results at all...
The same happens to my code, which has button due to onclick function
A Seg, 17 de ago de 2020, 07:27, Acuceno Chiconela <acuceno@gmail.com>
escreveu:
… It says nothing... only the button is shown
A Dom, 16 de ago de 2020, 20:01, Steve Griffith ***@***.***>
escreveu:
> ***@***.**** commented on this gist.
> ------------------------------
>
> 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?
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://gist.github.com/61ccf69758cd6dbdc429934564864650#gistcomment-3419343>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AOQJ46W3E5QWF6RVUD5SJQDSBANJLANCNFSM4QATQLPQ>
> .
>
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.
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
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