JQuery Parse XML method summary, jqueryxml Summary

Source: Internet
Author: User

JQuery Parse XML method summary, jqueryxml Summary

This example summarizes JQuery's XML parsing method. We will share this with you for your reference. The details are as follows:

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 using JQuery to parse XML.

Solution 1:

<script type="text/javascript">$(document).ready(function() {  $.ajax({     url: 'http://localhost/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>

Solution 2:

<script type="text/javascript">  $.get("http://localhost/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 xml files

$. Get ("xmlfile. xml", function (xml) {// xml indicates the content that can be read and used. For details, refer to 2nd });

2. Read xml content

If the read xml is used for the xml file, the processing is as follows:

$.get("xmlfile.xml",function(xml){   $(xml).find("item").length; });

If you read an xml string, note that the xml string must be surrounded by "<xml>" and "</xml>" before it can be parsed.

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

Parse xml content:

Example xml:

<? 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:

$ (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.