Processing XML data with namespaces in jquery _ jquery

Source: Internet
Author: User
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" %>




      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.