Phonegap应用中XML解析问题

4
我想在我的PhoneGap应用程序中解析XML Web服务,但我不知道如何做到这一点。我尝试了使用存储在远程服务器上的本地XML文件进行操作,这很有效,但当我调用服务器端XML文件时,我没有得到任何结果。如果有人有想法,请解决我的问题。如果有人有可行的简单代码,请发送给我。我已经尝试从Google中找到解决方案,但没有得到任何答案。请解决我的问题。我的服务器文件是http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml
2个回答

2
请在您的index.html文件中尝试以下代码,我已经在iPhone/Android模拟器中测试了该网址(http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml),它的运行非常顺畅。
<html>
<head>
<script src="js/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script>
function bodyload(){
    alert("We are calling jquery's ajax function and on success callback xml parsing are done");
$.ajax({  
    url:'http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml',  
    dataType:'application/xml', 
    timeout:10000,  
    type:'POST',  
    success:function(data) {
        $("#bookInFo").html("");
         $("#bookInFo").append("<hr>");
         $(data).find("Book").each(function () {
              $("#bookInFo").append("<br> Name: " + $(this).find("name").text());
              $("#bookInFo").append("<br> Address: " + $(this).find("address").text());
              $("#bookInFo").append("<br> Country: " + $(this).find("country").text());
              $("#bookInFo").append("<br><hr>");
         });
     },  
    error:function(XMLHttpRequest,textStatus, errorThrown) {     
      alert("Error status :"+textStatus);  
      alert("Error type :"+errorThrown);  
      alert("Error message :"+XMLHttpRequest.responseXML);
      $( "#bookInFo" ).append( XMLHttpRequest.responseXML);
    }
    });
}   
</script>
</head>
<body onload="bodyload()">
<button onclick="bodyload()">Ajax call</button>
<p id="bookInFo"></p>
</body>
</html>

输出- 输入图像描述

请注意一件重要的事情,将jQuery文件的插件名称更改为您端上存在的名称。 - Mayur Birari
你好,那些脚本文件不可用。请下载jquery.js并将其插入到您的HTML页面中。 - Mayur Birari
我遇到了“无传输”错误。http://www.jaysonragasa.net/files/test/resp.xml - Jayson Ragasa

0
使用这个:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" />
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script>

<script type="text/javascript">
        $(function() {

            $("#requestXML").click(function() {

                     $.ajax({
                      type: "POST",
                      url: "http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml",
                      data: "{}",
                      cache: false,
                      dataType: "xml",
                      success: onSuccess
                    });

            });

            $("#resultLog").ajaxError(function(event, request, settings, exception) {
              $("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
            });

            function onSuccess(data)
            {
            $("#resultLog").append(""+data);

                   $(data).find("Book").each(function () {
                        $("#resultLog").append("<br> Name: " + $(this).attr("name"));
                        $("#resultLog").append("<br> Address: " + $(this).attr("address"));
                        $("#resultLog").append("<br> Country: " + $(this).attr("country"));
                        $("#resultLog").append("<br><hr>");
                   });
               }
        });

    </script>

调用以上方法的按钮:

<input id="requestXML" type="button" value="Request XML" />

先生,不起作用,请查看我的错误 http://www.mediafire.com/?2ko925z4smk59u1 我没有得到任何输出,请帮忙。 - Sandeep
我已经更新了上面的答案,将js和css导入到你的html中。我更喜欢在模拟器或设备上运行它。 - Vinayak Bevinakatti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接