2011-09-23 82 views
4

我想在我的phonegap应用程序中解析xml webservice,但我不知道它是如何可能的。我试着用本地xml文件存储在远程服务器中,工作正常,但当我调用服务器端XML文件,然后我没有得到任何结果。请任何人有想法,然后请解决我的问题。请任何人有工作简单的代码,然后请发送给我。我试图从谷歌找出我的解决方案,但没有得到任何答案。请解决我的问题。我的服务器文件是http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml在phonegap应用程序中解析xml的问题

回答

2

请尝试以下您的index.html文件的代码,我已经测试过的网址( http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml)在iPhone/Android模拟器和它的工作如魅力。

<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> 

输出 - enter image description here

+0

1件重要的事情,请将jquery文件的插件名称更改为您的结尾处的一个。 –

+0

你好,这些脚本文件不可用。你会PLZ下载jquery.js,然后插入到您的HTML页面。 –

+0

我得到了“NO TRANSPORT”错误。 http://www.jaysonragasa.net/files/test/resp.xml –

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" /> 
+0

爵士,不工作请看到我的错误http://www.mediafire.com/?2ko925z4smk59u1我没有得到任何输出,请帮助我 – Sandeep

+0

上面的回答有更新,添加js和css导入到你的html。我更喜欢在模拟器或设备上运行它。 –