Several examples of jquery parsing XML programs

Source: Internet
Author: User

Parsing XML data with JavaScript is a common programming task that JavaScript can do, and jquery can certainly do. Let's summarize some examples of using jquery to parse XML.

The first scenario: Yanshi

<script type= "Text/javascript" >$ (document). Ready (function () {$.ajax ({  URL: ' http://www.nowamagic.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></ol></div>

The second scenario:

<script type= "Text/javascript" >$.get ("Http://www.nowamagic.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></ol></div>

The general steps are as follows:

1. Read the XML file

$.get ("XMLFile.xml", function (XML) {//xml is the content that can be read, read see 2nd});

2. Reading XML content

If the read XML is coming from an XML file, this is combined with the above point, which is handled as follows:

$.get ("XMLFile.xml", function (XML) {    $ (XML). Find ("item"). Length;    });

If you are reading an XML string, be aware that the XML string must be surrounded by "<xml>" and "</xml>" to be parsed

$ ("<xml><root><item></item></root></xml>"). Find ("item"). length;

Parsing XML content:

Sample xml:

<?xml version= "1.0" encoding= "Utf-8"?><fields> <field  name= "Name1" >    <fieldname> dsname</fieldname>    <datatype> characters </datatype>  </field>  <field name= "Name2" >    <fieldname>dstype</fieldname>    <datatype> characters </datatype>  </field> </fields>

Here's how to parse the sample code:

$ (XML). Find ("field"). each (function () {var field = $ (this); var fName = field.attr ("Name");//Read node attribute var dataType = Field.find ("datatype"). text ();//Read the value of the child node});

Several examples of jquery parsing XML programs

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.