[英]parse xml with javascript for html
我正在嘗試從Yahoo Console獲取有關天氣情況的一些信息。 Yahoo的xml結果樹如下所示: 結果樹
我所擁有的代碼能夠使我的工作深入到一個層次。 我的變量“ beta”用於標題,效果很好。 第二個變量是'alpha',它將不起作用。 我相信這與我如何稱呼它為“ item.condition.temp”以及節點值有關。
我對此很陌生,因此請列出您使用的任何來源,因為這將有助於我的前進。
<!DOCTYPE html>
<html>
<head>
<p id="alpha"></p>
<p id="beta"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
xhttp.open('GET', 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2444827&diagnostics=true', true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById('beta').innerHTML =
xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue;
document.getElementById('alpha').innerHTML =
xmlDoc.getElementsByTagName('item.condition.temp')[0].childNodes[0].nodeValue;
}
</script>
我建議將&format = json添加到您的URL中以使其達到此目的:
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2444827&diagnostics=true&format=json
然后解析生成的JSON,這是一種更JavaScript的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.