我是网络开发的新手,我想从网络服务器而不是本地服务器解析 xml 这怎么可能。我尝试了很多谷歌搜索。请帮助我。我必须解析这个 url“http://twitter.com/statuses/public_timeline.xml”
最佳答案
您可以在 HTML 页面中使用 JQuery ( http://jquery.com/) 插件作为脚本标记中的入口,调用 jquery 的 ajax() 函数来命中 url,您可以使用 jquery 的 parseXML() 和 find() 函数来解析 xml 数据 像这样-
<html>
<head>
<script src="js/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script>
<script>
function onBodyLoad(){
$.ajax({
url:'http://twitter.com/statuses/public_timeline.xml',
dataType:"xml",
contentType:'application/xml',
timeout:10000,
type:'POST',
success:function(data) {
alert(data);
var xmlDoc = $.parseXML( data ),
$xml = $( xmlDoc ),
$title = $xml.find( "title" );
/* append "RSS Title" to #someElement */
$( "#someElement" ).append( $title.text() );
/* change the title to "XML Title" */
$title.text( "XML Title" );
/* append "XML Title" to #anotherElement */
$( "#anotherElement" ).append( $title.text() );
},
error:function(XMLHttpRequest,textStatus, errorThrown) {
alert("Error status :"+textStatus);
alert("Error type :"+errorThrown);
alert("Error message :"+XMLHttpRequest.responseXML);
}});
}
</script>
</head>
<body onload="onBodyLoad()">
<p id="someElement"></p>
<p id="anotherElement"></p>
</body>
</html>
关于html - 如何将 xml 从服务器解析到我们的 phonegap 应用程序的 html 页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7496333/