If you are working on AJAX applications, you may often use jquery (or other frameworks) to process the data returned by the Service. It is quite convenient to use Jquery to process Json format. Unfortunately, the data returned by many services is still in XML format.
Jquery provides built-in support for processing xml data. The premise is that the returned data does not contain any namespaces. For example, the following data
The Code is as follows:
To process such data, the jquery code is roughly as follows:
The Code is as follows:
Var p = $ ("# placeholder ");
// Process xml without a namespace
$. Get ("data. xml", null, function (data ){
Var employees = $ ("Employee", data); // locate all the Employee nodes
Var ul = $ ("
");
Employees. each (function (){
$ ("
"). Text ($ (this ). attr ("firstName") + "" + $ (this ). attr ("lastName ")). appendTo (ul); // construct a new li tag for each row of data and insert it into ul
});
Ul. appendTo (p );
});
However, if our XML data has a namespace, the above Code will be invalid. The reason is that jquery cannot handle namespaces by default \
The Code is as follows:
To solve this problem, some enthusiastic netizens wrote a jquery plug-in called jquery. xmlns. js. If you are interested, you can learn and download it below.
Http://www.rfk.id.au/blog/entry/xmlns-selectors-jquery/
Then, we can use the following method to solve the problem:
The Code is as follows:
$. Xmlns ["d"] = "http://tech.xizhang.com ";
// Process xml with namespace
$. Get ("datawithnamespace. xml", null, function (data ){
Var employees = $ ("d | Employee", data); // locate all the Employee nodes
Var ul = $ ("
");
Employees. each (function (){
$ ("
"). Text ($ (this). attr (" firstName ") +" "+ $ (this). attr (" lastName "). appendTo (ul );
});
Ul. appendTo (p );
});
I have to say that the namespace in the XML technical specification is really a bad design. It adds a lot of trouble, better than the benefits it brings.
The complete code of this example is as follows:
The Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "WebApplication1.WebForm1" %>