Jquery parses XML strings and XML files

  • 2020-03-30 02:04:02
  • OfStack

1. Read XML string:
Such as:


$(document).ready(function(){ 
var xml = "<xml><root><record><name>liubl</name></record><record><name>chencp</name</record></root></xml>";
//var xml="<table><tr><td>100<td></tr></table>"; // Can also be recognized directly Table
var obj = $(xml).find("record"); 
});

Note: make sure you have a layer of XML on the outside for jquery to be recognized, and you also need to have a root node in the XML that is root or something customizable.

2. Read the XML file
Such as:


$(document).ready(function(){ 
$.get('mydata.xml',function(xml)
{
$(xml).find("book").each(function(){
var xmlAttr=$(this).attr(" attribute ") ; 
var xmlText=$(this).fint(" node ").text() ; 
});
});
});


Related articles: