"JQuery" JQ handles XML files and XML strings

Source: Internet
Author: User

1.xml file

<?xml version= "1.0" encoding= "Utf-8"?> <root>     <book id= "1" >         <name> Sharp jquery1</ name>         <author>XXX1</author>         <price>250</price>     </book>     < Book id= "2" >         <name> sharp jquery2</name>         <author>XXX2</author>         <price> 99</price>     </book>     <book id= "3" >         <name> sharp jquery3</name>         < author>xxx3</author>         <price>108</price>     </book>     <book id= "4" >         <name> Sharp jquery4</name>        <author>XXX4</author>         <price>128</ price>     </book> </root>  

2.jQuery Code

 Window.onload = function () {//get parse XML file $.get ("1.xml", function (data) {$data =                $ (data);                Get the node 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");//Get Books child nodes                Get the attribute, node text//var id = $ ($books [0]). attr ("id") = $books [0].getattribute ("id");//Get the id attribute of the first book                $authors. each (function () {var Author_text = $ (this). text ();//Get the inside of the node, try to use nodevalue without fruit (incompatible) })//ajax parse XML file $.ajax ({url: "1.xml", data                    Type: "xml", Success:function (data) {var $data = $ (data);                    var $books = $data. Find ("book");     The following actions are as follows}       })        } 

3.jQuery Processing of XML strings

jquery parsing xml string            var xmlstr = "<root><book id= ' 1 ' ><name> sharp Jquery1</name><author >xxx1</author><price>250</price></book><book id= ' 2 ' ><name> sharp jquery2< /name><author>xxx2</author><price>99</price></book><book id= ' 3 ' ><name > Sharp jquery3</name><author>xxx3</author><price>108</price></book>< Book id= ' 4 ' ><name> sharp 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 ());            Other operations are the same as 2

"JQuery" JQ handles XML files and XML strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.