如何使用javascript显示存储在xml文档中的图像链接?



如果我在XML标签中有一个图像地址,我如何使用javascript在HTML中显示图像?如果可能的话。

的XML

<countries>
<country region="north-america">
<name>USA</name>
<totalcases>88,822,018</totalcases>
<activecases>3,247,166</activecases>
<seriouscases>3,117</seriouscases>
<totalrecoverd>84,534,008</totalrecoverd>
<deaths>1,040,844</deaths>
<casespermill>265,263</casespermill>
<flag>https://cdn.britannica.com/33/4833-004-828A9A84/Flag-United-States-of-America.jpg</flag>
</country>
</countries>
<html>
<head>
<script>
const xhr = new XMLHttpRequest();
xhr.onload = function () {
let image = xhr.responseXML.documentElement.getElementsByTagName('flag')[0].childNodes[0];
document.body.append(image.data);
console.log(image);
};
xhr.onerror = function () {
console.log('Error while getting XML.');
};
xhr.open('GET', 'Folder/data.xml'); //<<==== correct path to your XML file
xhr.responseType = 'document';
xhr.send();
</script>
</head>
</html>

最新更新