用于解析 xml 文档的 Javascript 代码

标签 javascript android cordova restful-authentication alfresco

我想要一个javascript代码,用于使用Phonegap并编写返回结果并将此返回结果附加到alfresco的webscript的其他调用中(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>

最佳答案

对于 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!!!");
    }
});

如果对您有帮助,请告诉我..

关于用于解析 xml 文档的 Javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493419/

相关文章:

cordova - Ionic 2 - 'import' 和 'export' 可能仅与 'sourceType: module' 一起出现

javascript - 将 jQuery 与 'use strict' 结合使用的正确方法是什么?

cordova - Sencha Touch 2.1 升级 - Web 应用程序和移动应用程序在 iOS 模拟器上出现白屏

javascript - 如何根据用户的操作更改 div 背景颜色?

java - 复制 View 以创建 View 的精确副本。相对于屏幕移动 View

android - 无法在额外属性扩展上获取属性 'ndkVersion',因为它不存在于... react-native 0.64.0

java - Firestore执行复合查询,事件 "MODIFIED"未拦截

android - 错误 : Attempting to call cordova. exec() 在 'deviceready' 之前。无视

javascript - 如何开始使用 Fabric.js

javascript - 用于文件上传的 HTML 表单 - 由 JavaScript 生成