jQuery操作XML入门

来源:互联网 发布:linux远程重启服务器 编辑:程序博客网 时间:2024/06/06 01:34

 XMLFile.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <msglist>
  3.   <msg name="11">
  4.     <id>1</id>
  5.     <content>content1</content>
  6.   </msg>
  7.   <msg name="22">
  8.     <id>2</id>
  9.     <content>content2</content>
  10.   </msg>
  11. </msglist>

jqXmlFirst.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="jqXmlFirst.aspx.cs" Inherits="jqueryXml_jqXmlFirst" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>jQuery操作Xml入门</title>
  6.     <script type="text/javascript" src="../js/jquery-1.2.6.js"></script>
  7.     <script type="text/javascript">
  8.         $(function(){
  9.             $.ajax({
  10.                 url:"XMLFile.xml",
  11.                 dataType:"xml",
  12.                 error: function(xml){
  13.                     alert('Error loading XML document'+xml);
  14.                 },
  15.                 success:function(xml){
  16.                     $(xml).find("msglist > msg").each(function(){
  17.                         alert($(this).find("content").text());//each是循环执行,即多次弹出。
  18.                         alert($(this).attr("name"));//取得属性的方法
  19.                     });
  20.                 }
  21.             })
  22.         });
  23.     </script>
  24. </head>
  25. <body>
  26.     <form id="form1" runat="server">
  27.     <div>
  28.     
  29.     </div>
  30.     </form>
  31. </body>
  32. </html>
end
原创粉丝点击