【jQuery】jQ處理xml檔案和xml字串

來源:互聯網
上載者:User

標籤:alt   each   value   class   gif   odi   屬性   nod   str   

1.xml檔案

<?xml version="1.0" encoding="utf-8" ?> <root>     <book id="1">         <name>鋒利的jQuery1</name>         <author>XXX1</author>         <price>250</price>     </book>     <book id="2">         <name>鋒利的jQuery2</name>         <author>XXX2</author>         <price>99</price>     </book>     <book id="3">         <name>鋒利的jQuery3</name>         <author>XXX3</author>         <price>108</price>     </book>     <book id="4">         <name>鋒利的jQuery4</name>        <author>XXX4</author>         <price>128</price>     </book> </root>  

2.jQuery代碼

 window.onload = function () {            //get解析xml檔案            $.get("1.xml", function (data) {                $data = $(data);                //獲得節點                var $books = $data.find("book");                var $names = $data.find("name");                var $authors = $data.find("author");                //var $authors = $data.find("author") = $books.children("author") = $names.siblings("author");//獲得books的子節點                //獲得屬性、節點text                //var id = $($books[0]).attr("id") = $books[0].getAttribute("id");//獲得第一個book的id屬性                $authors.each(function () {                    var author_text = $(this).text(); //獲得節點內部文本,嘗試過用nodeValue無果(不相容)                })            })            //ajax解析xml檔案            $.ajax({                url: "1.xml",                dataType: "xml",                success: function (data) {                    var $data = $(data);                    var $books = $data.find("book");                    //以下操作同上                }            })        }

 3.jQuery處理xml字串

//jQuery解析xml字串            var xmlStr = "<root><book id=‘1‘><name>鋒利的jQuery1</name><author>XXX1</author><price>250</price></book><book id=‘2‘><name>鋒利的jQuery2</name><author>XXX2</author><price>99</price></book><book id=‘3‘><name>鋒利的jQuery3</name><author>XXX3</author><price>108</price></book><book id=‘4‘><name>鋒利的jQuery4</name><author>XXX4</author><price>128</price></book></root>";            var $xmlDoc = $($.parseXML(xmlStr));            var $books = $xmlDoc.find("book");            var $names = $xmlDoc.find("name");            alert($($names[0]).text());            //其他動作與2相同

 

【jQuery】jQ處理xml檔案和xml字串

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.