如何从API获取xml值
问题描述:
我试图显示一些包含在API中的值(此api被称为PRTG的程序用于从程序中提取信息),这是API如何从API获取xml值
这个API给予的你的XML文件看起来像这样。
<?xml version="1.0" encoding="UTF-8"?>
<channels totalcount="0" listend="1">
<prtg-version>16.4.27.6720</prtg-version>
<item>
<name>Tiempo de inactividad</name>
</item>
<item>
<name>Voltaje Bateria</name>
<lastvalue>54,51 VDC</lastvalue>
<lastvalue_raw>0000000000054510.0000</lastvalue_raw>
</item>
</channels>
我想用JavaScript来取标签lastvalue价值,并在网页上显示的话,我都试过,但没有在所有工作
答
我怀疑你需要的JavaScript,因为结果应该显示在这一页。如果你调用API只是改变ajax部分,一切都应该工作。为了模仿API的一些响应,我在文件上做了这个。
<html>
<head>
<title>Demo</title>
</head>
<body>
<p id="result"></p>
<script>
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "channels.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, txt, xmlDoc, node;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("item");
for (i = 0; i < x.length; i++) {
node = x[i].getElementsByTagName('lastvalue');
if (node && node.length > 0){
txt += node[0].textContent + "<br>";
}
}
document.getElementById("result").innerHTML = txt;
}
</script>
</body>
</html>
是否与Java有关的问题时,JavaScript或jQuery的。请更加清楚 – Joe
欢迎来到SO。请显示您尝试过的代码,并告诉代码做了什么不是你想要的。 – LarsH
我尝试过使用javascript,使用requestAjax。 –