简体   繁体   English

XML在jquery中格式不正确

[英]XML is not well-formed in jquery

I have called an ajax function that gather data from an xml file formatted like this 我已经调用了ajax函数,该函数从格式如下的xml文件中收集数据

<?xml version="1.0" encoding="utf-8"?>
<values>     
  <value date="2015-07-12">37.170</value>
  <value date="2015-07-13">7.190</value>
  <value date="2015-07-12">37.170</value>
  <value date="2015-07-12">3.210</value>
  <value date="2015-07-12">37.20</value>
</values>

When I get the response from the console it says not well-formed :1:80 当我从控制台获得响应时,它说格式不正确:1:80

Not sure what this means since when I use xml validator they all tell me that there are no errors. 不知道这意味着什么,因为当我使用xml验证器时,它们都告诉我没有错误。 Any help would be awesome to have. 任何帮助都会很棒。

        var data = [];
    $.ajax({
        url: 'test.xml', // name of file you want to parse
        dataType: "xml", // type of file you are trying to read
        success: parse, // name of the function to call upon success
        async:    false,
        error: function(xhr, status, error) {
              alert(xhr.responseText);
              console.log("readyState: " + xhr.readyState);
                console.log("responseText: "+ xhr.responseText);
                console.log("status: " + xhr.status);
                console.log("text status: " + textStatus);
                console.log("error: " + err);
        }
    });

    function parse(xml) {
        var items = jQuery(xml).find("values");
        var data = [];
        var values = $(items).text();
        $(document).find("Values").each(function () {
            $(this).find("value").each(function () { data.push($this.text);});
        });
        console.log(data.length);
    }

Not sure ethier how to get all of the values into the array. 不知道如何将所有值都放入数组。

EDIT! 编辑!

Fiddle is updated. 小提琴已更新。

http://jsfiddle.net/b62tx8Ln/1/ http://jsfiddle.net/b62tx8Ln/1/

No need to parse data with parseXML. 无需使用parseXML解析数据。

Erik Phillips is probably right about the double parsing. Erik Phillips可能对双重解析是正确的。 Anyways, this should work. 无论如何,这应该可行。

Note the use of ECHO in fiddle..... just to simulate a local xml-file. 注意在提琴中使用ECHO .....只是为了模拟本地xml文件。

 $.ajax({
        url: 'test.xml', // name of file you want to parse
        dataType: "xml", // type of file you are trying to read
        success: parse, // name of the function to call upon success
        async:    false,
        error: function(xhr, status, error) { 
            alert("error");
        }
    });

    function parse(xmldata) {
        $(xmldata).find("value").each(function(){
            alert($(this).text());
        });
    }

You may be trying to parse it twice, per the documentation: 您可能会尝试根据文档将其解析两次:

The type of data that you're expecting back from the server. 您期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). 如果未指定,则jQuery将尝试根据响应的MIME类型来推断它(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4中,脚本将执行该脚本,而其他所有内容将是以字符串形式返回)。

Thus I'm imagining that this is what you are looking for: 因此,我在想这就是您要寻找的东西:

function parse(xml) {
  //var items = jQuery(xml).find("values");
  var items = xml.find("values");
  var data = [];
  var values = $(items).text();
  $(document).find("Values").each(function () {
    $(this).find("value").each(function () { data.push($this.text);});
  });
  console.log(data.length);
}

For anybody else having a problem parsing non-well-formed (self-closing tags) xml in IE, $(xmlthing).find('nodename').each... works, whereas IE will parse $(xmlthing).children('nodename').each... (which is fine in FF and Chrome), as nested. 对于在解析IE中格式不正确的(自关闭标签)xml时遇到问题的其他任何人, $(xmlthing).find('nodename').each...可以工作,而IE可以解析$(xmlthing).children('nodename').each... (在FF和Chrome中很好用),嵌套。

That is: 那是:

<thing /> <thing /> <thing />

will be 3 things in FF/Chrome, but one in IE if you use $(node).children(thing) 在FF / Chrome中将是3件事,但是在IE中,如果您使用$(node).children(thing)

$(node).find(thing) will give you 3 things in all browsers. $(node).find(thing)将在所有浏览器中为您提供3件东西。 Thanks @Steen 谢谢@Steen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM