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