阅读XML的jQuery [英] read xml by jquery

查看:100
本文介绍了阅读XML的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML

 <?xml version='1.0' encoding='utf-8' ?>
        <RecentTutorials>
        <Tutorial author='The Reddest'>
        <Title>Silverlight and the Netflix API</Title>
        <Categories>
              <Category>Tutorials</Category>
              <Category>Silverlight 2.0</Category>
              <Category>Silverlight</Category>
              <Category>C#</Category>
              <Category>XAML</Category>
        </Categories>
        <Date>1/13/2009</Date>
        </Tutorial>
        </RecentTutorials>

剧本

$.ajax({
   type: "post",
   url: "Default.aspx?cmd=Setting",
   success: parseXml
});


    alert(xml);//show xml File Success
    $(xml).find("Tutorial").each(function()
    {
         $("#b").append($(this).attr("author") );
    }

XML文件都看不懂虽然警报(XML);显示XML文件

XML files have not read While alert(xml); show XML File

推荐答案

而不是 $(),使用的 $。parseXML 解析XML字符串。 (更新:查看下面的注释,在jQuery的1.5加入 parseXML 但它很容易,如果你想将它添加到一个旧版本),它会给你一个原始XML文档;然后你会使用 $()就可以得到关于DOC一个jQuery的包装。

Rather than $(), use $.parseXML to parse an XML string. (Update: See note below, parseXML was added in jQuery 1.5 but it's easy to add it to an older version if you want.) It will give you a raw XML document; you'd then use $() on it to get a jQuery wrapper on that doc.

这样的:

var xml =
    "<?xml version='1.0' encoding='utf-8' ?>" +
    "<RecentTutorials>" +
    "<Tutorial author='The Reddest'>" +
    "<Title>Silverlight and the Netflix API</Title>" +
    "<Categories>" +
    "<Category>Tutorials</Category>" +
    "<Category>Silverlight 2.0</Category>" +
    "<Category>Silverlight</Category>" +
    "<Category>C#</Category>" +
    "<Category>XAML</Category>" +
    "</Categories>" +
    "<Date>1/13/2009</Date>" +
    "</Tutorial>" +
    "</RecentTutorials>";

$($.parseXML(xml)).find("Tutorial").each(function() {
    var author = $(this).attr("author");
});

活生生的例子

如果您通过加载 AJAX ,通常不必做,因为jQuery将为你做它作为加载程序的一部分,然后给你的XML文档的数据参数到你的成功的回调,但如果你只是有一个任意的字符串,你要分析它, parseXML 是该工具的工作。

If you're loading the XML via ajax, you typically don't have to do that as jQuery will do it for you as part of the loading sequence and then give you the XML doc as the data parameter to your success callback, but if you just have an arbitrary string and you want to parse it, parseXML is the tool for the job.

AJAX 例如:

$.ajax({
  url: "the/url/to/load/the/xml/from",
  method: "GET",
  dataType: "xml",
  success: function(data) {
      var xdoc = $(data);    // Note that jQuery has already done the parsing for us
      display("Getting tutorials");
      var tutorials = xdoc.find("Tutorial");
      display("Found: " + tutorials.length);
      tutorials.each(function() {
          display("Tutoral author: " + $(this).attr("author"));
      });
  },
  error: function(jxhr, status, err) {
      display("Ajax error: status = " + status + ", err = " + err);
  }
});

活拷贝

更新 parseXML 加入jQuery的V1.5中。如果可以的话,升级到最新的使用它。如果你不能,如果你的有无的使用一个过时的版本,您可以轻松地添加功能,以您自己的脚本。不像jQuery的许多地方,这是很好的自包含的jQuery的源代码:

Update: parseXML was added to jQuery in v1.5. If you can, upgrade to the latest to use it. If you can't, if you have to use an out-dated version, you can easily add the function to your own script. Unlike many parts of jQuery, it's nicely self-contained in the jQuery source:

jQuery.parseXML = function( data , xml , tmp ) {

    if ( window.DOMParser ) { // Standard
        tmp = new DOMParser();
        xml = tmp.parseFromString( data , "text/xml" );
    } else { // IE
        xml = new ActiveXObject( "Microsoft.XMLDOM" );
        xml.async = "false";
        xml.loadXML( data );
    }

    tmp = xml.documentElement;

    if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
        jQuery.error( "Invalid XML: " + data );
    }

    return xml;
};

这是我的第一个例子中的实时拷贝,但使用jQuery 1.4.4加上上面。

Here's a live copy of my first example, but using jQuery 1.4.4 plus the above.

这篇关于阅读XML的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆