2012-02-29 84 views
1

我想要一個javascript代碼來遠程調用alfresco的web腳本(http://10.0.2.2:8080/alfresco/service/api/login?u=admin & pw = admin) Android應用程序使用Phonegap並刪除返回結果並將此返回結果附加到其他外部web腳本(http:// localhost:8080/alfresco/service/sample/folder/Company%20Home?format = atom)的調用。 請幫助我。解析xml文檔的Javascript代碼

我有這個示例代碼,我怎麼能改變它到我的需要...我想調用http://10.0.2.2:8080/alfresco/service/api/login?u=admin&pw=admin解析返回值,並將它附加到webscript http://localhost:8080/alfresco/service/sample/folder/Company%20Home?format=atom的一個更多的調用。

<html> 
    <head> 
    <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script> 
    <script src="jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script> 
    <script src="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', 
       url:' 
        dataType:'application/xml', 
        timeout:10000, 
        type:'POST', 

        success:function(data) { 
        console.log("Customers Tab",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()">Get Ticket</button> 
    <p id="bookInFo"></p> 
</body> 

+0

我還沒有明確的想法關於Javascript,因爲我探討了在android u中解析xml唱手機通過調用Ajax調用和jquery完成(http://www.kumarchetan.com/blog/2012/01/14/my-first-android-app-or-how-to-develop-an-android-app -using-phonegap-and-jquery /)。我無法將它與我的需求聯繫起來......已經厭倦了看到上面的示例示例... – Surya 2012-02-29 04:09:35

+0

您可以使用E4x在javascript中進行xml解析:http:// www .w3schools.com/xml/xml_e4x.asp – 2012-02-29 07:05:03

+0

@MatjazMuhic這是否支持使用Phonegap的android應用程序? – Surya 2012-02-29 07:10:05

回答

0

對XML喜歡

<test> 
    <something> 
     <data>Data1</data> 
     <other>Other1</usage> 
    </something> 
    <something> 
     <data>Data1</data> 
     <other>Other1</usage> 
    </something> 
</test> 

使用這樣解析

$.ajax({ 
    type : 'GET', 
    url : "http://some-url:8080/test.xml", 
    data : { 
     key : "value" 
    }, 
    dataType : "xml", 
    success : function(xml) { 

     data1 = $(xml).find('data').eq(0).text(); 
     data2 = $(xml).find('data').eq(1).text(); 

     other1 = $(xml).find('other').eq(0).text(); 
     other2 = $(xml).find('other').eq(1).text(); 

    }, 

    error : function(xhr) { 
     alert("Error while loading!!!"); 
    } 
}); 

讓我知道,如果它幫助你..