Javascript код для разбора XML-документа - PullRequest
1 голос
/ 29 февраля 2012

Мне нужен код JavaScript для вызова веб-скрипта (http://10.0.2.2:8080/alfresco/service/api/login?u=admin&pw=admin) удаленно в приложении для Android, используя Phonegap , вставьте возвращаемый результат и добавьте этот возвращаемый результат к другому вызову веб-скрипта alfresco (http://localhost:8080/alfresco/service/sample/folder/Company%20Home?format=atom).. пожалуйста, помогите мне в этом.

У меня есть этот пример кода, как я могу изменить его в соответствии со своими потребностями ... Я хочу вызвать 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>

1 Ответ

0 голосов
/ 29 февраля 2012

Для 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!!!");
    }
});

Дайте мне знать, помогло ли это вам ...

...