我正在尝试从Yahoo控制台获取有关天气状况的一些信息。来自Yahoo的xml结果树如下所示:result tree
我能够使用我的代码获得只有一层深度的东西。我的变量'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>
答案 0 :(得分:0)
我建议将&amp; format = json添加到您的网址中以实现此目的:
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2444827&diagnostics=true&format=json
然后解析生成的JSON,这是一种更简单的JavaScript方式。