1、要解析的xml文件booklist.xml
2、解析并显示的html
3、结果
- <?xml version="1.0" encoding="UTF-8"?>
- <booklist>
- <book isbn='b001'>
- <name>java</name>
- <author>Gaosilin</author>
- </book>
- <book isbn='b002'>
- <name>Python</name>
- <author>卡卡</author>
- </book>
- </booklist>
2、解析并显示的html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Jquery解析xml</title>
- <script src="jquery.js"></script>
- <script type="text/javascript">
- loadbook();
- function loadbook(){
- //使用Ajax方式加载后再处理
- $.ajax({
- type:'GET',
- url:'booklist.xml',
- dataType:'xml',//注意处理信息的类型为xml,默认为html
- success:function(docxml){
- //查找并迭代<book>节点
- $(docxml).find('book').each(function(){
- var isbn = $(this).attr('isbn');//获取节点属性
- var bookname = $(this).children('name').text();//获取子节点的文本值
- var author = $(this).children('author').text();
- //构造节点
- var bookdiv = '<div><li>ISBN:'+isbn+'</li><li>书名:'+bookname+'</li><li>作者:'+author+'</li><div><br>';
- $('#listdiv').append(bookdiv);
- })
- }
- });
- }
- </script>
- </head>
- <body>
- <div id="listdiv">
- </div>
- </body>
- </html>
3、结果