Parsing XML data with JavaScript is a common programming task. JavaScript can do it, And JQuery can certainly do it. Below we will summarize several examples of parsing XML using JQuery.
Solution 1: the code is as follows: <script type = "text/javascript" >$ (document ). ready (function () {$. ajax ({url: 'http: // www.test.net/cgi/test.xml', dataType: 'xml', success: function (data) {// console. log (data); $ (data ). find ("channel "). find ("item "). each (function (index, ele) {var titles = $ (ele ). find ("title "). text (); var links = $ (ele ). find ("link "). text (); console. log (titles + '-----'); $ ("# noticecon "). find ('ol '). append ('<li> <A href = "'+ links +'"> '+ titles +' </a> </li> ');});}});}) </script> <div id = "noticecon"> <ol> </div> solution 2: the code is as follows: <script type = "text/javascript"> $. get ("http://www.test.net/cgi/test.xml", function (data) {$ (data ). find ('channel '). find ('item '). each (function (index, ele) {var titles = $ (ele ). find ('title '). text (); var links = $ (ele ). find ('link '). text (); $ ("# noticecon "). find ('ol '). append ('<li> <a href = "'+ Links +'"> '+ titles +' </a> </li> ');})}); </script> <div id = "noticecon"> <ol> </div> the general steps are as follows: 1. the code for reading the xml file is as follows: $. get ("xmlfile. xml ", function (xml) {$ (xml ). find ("item "). length;}); 2. if the xml file is used to read the xml content, the Code is as follows: $. get ("xmlfile. xml ", function (xml) {$ (xml ). find ("item "). length;}); if you are reading an xml string, pay attention to the following points, the xml string must be surrounded by "<xml>" and "</xml>" to parse the Code as follows: $ ("<xml> <root> <item> </root> </xml> "). f Ind ("item"). length; Parse xml content: sample xml: the code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?> <Fields> <field Name = "Name1"> <fieldname> dsname </fieldname> <datatype> character </datatype> </field> <field Name = "Name2"> <fieldname> dstype </fieldname> <datatype> character </datatype> </field> </fields> The following is the parsing sample code: the Code is as follows: $ (xml ). find ("field "). each (function () {var field = $ (this); var fName = field. attr ("Name"); // read the node attribute var dataType = field. find ("datatype "). text (); // read the value of the subnode });