日期:2014-05-16 浏览次数:20358 次
<?xml version="1.0"?> <msglist> <folder> <name>folder1</name> <file>file1</file> <folder1> <name>folder1.1</name> <file>file1.1</file> </folder1> </folder> <folder> <name>folder2</name> <file>file2.1</file> </folder> <folder> <name>folder3</name> </folder> <folder> <name>folder4</name> <file>file4.1</file> <file>file4.2</file> </folder> </msglist>
<html> <head> <script type="text/javascript" src="jquery.js"> </script> <script> $.ajax({ type: "POST", // 默认:GET 请求方式:[POST/GET] dataType: "xml", //默认["xml"/"html"] 返回数据类型:["xml" / "html" / "script" / "json" / "jsonp"] url: "guestbook.xml", // 默认当前地址,发送请求的地址 data: { key: "value" }, // 发送到服务器的数据 error: function(xml) { alert('Error loading XML document' + xml); }, // 请求失败时调用 success: function(xml) { // 请求成功后回调函数 参数:服务器返回数据,数据格式. $("#dictionary").empty(); // 用Jquery处理xml数据 $(xml).find('msglist').each(function() { //var folder = $(this).find('folder > name').text(); //var file = $(this).find('folder > file').text(); //$('#dictionary').append('<li>' + folder + '</li>'); }); } }) </script> </head> <body> <div id = dictionary> </div> </body> </html>
<li><span class="folder">folder1</span> <ul> <li><span class="file">file1</span></li> <ul> <li><span class="folder">folder1.1</span> <ul> <li><span class="file">file1.1</span></li> </ul> </li> </ul> </ul> </li> <li><span class="folder">folder2</span> <ul> <li><span class="file">file2.1</span></li> </ul> </li> <li><span class="folder">folder3</span> </li> <li><span class="file">File 4</span></li> ······