返回 xml 字符串 - 如何使用 JQuery/Json 解析 xml 文件

发布于 2024-10-05 10:54:59 字数 1327 浏览 1 评论 0原文

当我提醒它返回这样的字符串时:

data    "<?xml version="1.0" encoding="utf-8" ?> 
      <xml xmlns="http://www.opengis.net/kml/2.2">
      <Document>
      <Name>John Smith</Name> 
      <Description>stackoverflow</Description> 
      <Total>50</Total> 
      </Document>
      </xml>"

更新: 我尝试使用此方法 getJSON 并且确实收到警报,但从未在 find('Document').each..... 中执行

 $.getJSON(_url, function (data) {

            alert(data);    
            $(data).find('Document').each(function () {
                debugger
                var name = $(this).find('Name');
                var desc = $(this).find('Description').text();
                var total = $(this).find('Total').text()

            });

        });

如何在 jquery 中读取 xml 文件,下面是返回给我的内容作为一个字符串,当我执行警报(数据)时我可以看到它;

 $.getJSON(url, {},
                function (data) {
                    alert(data);
             }
});


<?xml version="1.0" encoding="utf-8" ?> 
- <xml xmlns="http://www.opengis.net/kml/2.2">
- <Document>
  <Name>John Smith</Name> 
  <Description>stackoverflow</Description> 
  <Total>50</Total> 
  </Document>
  </xml>

when i do alert it returning string like this:

data    "<?xml version="1.0" encoding="utf-8" ?> 
      <xml xmlns="http://www.opengis.net/kml/2.2">
      <Document>
      <Name>John Smith</Name> 
      <Description>stackoverflow</Description> 
      <Total>50</Total> 
      </Document>
      </xml>"

Update:
i tried using this method getJSON and i do get alerts but never execute inside the find('Document').each.....

 $.getJSON(_url, function (data) {

            alert(data);    
            $(data).find('Document').each(function () {
                debugger
                var name = $(this).find('Name');
                var desc = $(this).find('Description').text();
                var total = $(this).find('Total').text()

            });

        });

how to read xml file in jquery, below is what is returning me as a string and i can see that when i do alert(data);

 $.getJSON(url, {},
                function (data) {
                    alert(data);
             }
});


<?xml version="1.0" encoding="utf-8" ?> 
- <xml xmlns="http://www.opengis.net/kml/2.2">
- <Document>
  <Name>John Smith</Name> 
  <Description>stackoverflow</Description> 
  <Total>50</Total> 
  </Document>
  </xml>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人心善变 2024-10-12 10:55:00

其他人说你不应该使用 JSON,他们是正确的,但我认为你真正需要知道的是 XML 可以像 HTML 一样使用 jQuery 进行导航。您可以使用 $('Name') 等选择器来获取 数据等。因此,一旦您返回了数据,您就可以执行以下操作:

var people = data.children('Name');

Others have said that you should not be using JSON, and they are correct, but I think what you really need to know is that XML can be navigated just like HTML using jQuery. You can use selectors like $('Name') to get at your <Name> data and so on. So once you have your data returned, you can do something like this:

var people = data.children('Name');
久夏青 2024-10-12 10:54:59

如果您仍在寻找答案,Google 的 ajax API 有一个内置的 xml->json 转换器。

您可以通过 http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q= 调用它
最后加上您的请求网址。

如果您尝试使用 JSONP 并解决同源问题,它看起来会像这样:

var googleAPI = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=";

$.getJSON(googleAPI + url + "&callback=?", null, function(data) {

        alert(data);    
        $(data).find('Document').each(function () {
            debugger
            var name = $(this).find('Name');
            var desc = $(this).find('Description').text();
            var total = $(this).find('Total').text()

        });
});

但是,这将为您提供 JSON 数据,因此您将需要修改回调以序列化它并访问名称、描述、总计元素作为属性。如果您需要这方面的指导,请查看在 jQuery 中序列化为 JSON

If you're still looking for an answer, Google's ajax API has a built in xml->json converter.

You can call it via http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=
with your request url at the end.

If youre trying to use JSONP and get around same origin concerns, it would look something like this:

var googleAPI = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=";

$.getJSON(googleAPI + url + "&callback=?", null, function(data) {

        alert(data);    
        $(data).find('Document').each(function () {
            debugger
            var name = $(this).find('Name');
            var desc = $(this).find('Description').text();
            var total = $(this).find('Total').text()

        });
});

However, this will give you JSON data, so youre going to need to modify your callback to serialize it and access the Name, Description, Total elements as attributes. If you need direction on this, checkout Serializing to JSON in jQuery

彻夜缠绵 2024-10-12 10:54:59

您似乎误解了 JSON 是什么,以及它如何在 jQuery 中使用!?

如果要做跨域,返回的数据必须是JSON格式。 jQuery 将在收到 JSON 后立即尝试解析它。它期望它的格式类似于“jsonp1291171891383({})”,然后将其评估为 JavaScript。您返回的 XML 不是 JavaScript。

解决此问题的一种可能方法是返回数据类似于“jsonp1({"data":""})"。如果是这种情况,那么在您的示例中,变量“data”是纯文本,您需要先解析 XML,然后才能通过选择器方法访问它。

来自此处

jQuery.fromXMLString = function(strXML){
    if (window.DOMParser) {
        return jQuery(new DOMParser().parseFromString(strXML, "text/xml"));
    } else if (window.ActiveXObject) {
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(strXML);
        return jQuery(doc);
    } else {
        return jQuery(strXML);
    }
};

然后在你的代码中:

 $.fromXMLString(data).find('Document').each( ... );

You appear to have misunderstood what JSON is, and how it is used in jQuery!?

If you want to do cross-domain, the returned data must be in JSON format. jQuery will attempt to parse your JSON as soon as it receives it. It expects it in a format like "jsonp1291171891383({})" which is then evaluated as JavaScript. The XML you have returned in not JavaScript.

One possible way to work around this is your return data is something like "jsonp1({"data":"<xml>"})". If that is the case, then in your example the variable "data" is plain text, and you will need to parse the XML before you can access it via selector methods.

From here.

jQuery.fromXMLString = function(strXML){
    if (window.DOMParser) {
        return jQuery(new DOMParser().parseFromString(strXML, "text/xml"));
    } else if (window.ActiveXObject) {
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(strXML);
        return jQuery(doc);
    } else {
        return jQuery(strXML);
    }
};

And then in your code:

 $.fromXMLString(data).find('Document').each( ... );
国际总奸 2024-10-12 10:54:59

您不应该将 .getJSON 用于 XML 数据。相反,试试这个:

$.ajax({
    url: url,
    data: {},
    success: function(data){
        // now you can traverse your data just like the DOM
        // e.g. 
        // alert( $(data).find('Document:first Name').text() );
    },
    dataType: 'xml'
});

You should not be using .getJSON for XML data. Instead, try this:

$.ajax({
    url: url,
    data: {},
    success: function(data){
        // now you can traverse your data just like the DOM
        // e.g. 
        // alert( $(data).find('Document:first Name').text() );
    },
    dataType: 'xml'
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文