Xml string parsing example using jquery

Source: Internet
Author: User

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 });

Related Article

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.