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