Home Web Front-end JS Tutorial Processing XML data with namespaces in jquery_jquery

Processing XML data with namespaces in jquery_jquery

May 16, 2016 pm 06:06 PM
xml data Namespaces

But unfortunately, the data returned by many services is still in XML format.
jquery has built-in support for processing data such as xml, and there is no problem with this. But the premise is that the returned data does not have any namespace. For example, the following data

Copy code The code is as follows:





< /Employee>






To process such data, the jquery code is roughly as follows
Copy code The code is as follows:

var div = $("#placeholder");
// Process xml without namespace
$.get("data.xml", null, function (data) {
var employees = $("Employee", data); //Find all 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(div);
    });

But if our XML data has a namespace, the above code will be invalid. The reason is that jquery cannot handle the namespace
by default. Copy the code . The code is as follows:











< d:Employee id="1" firstName="bill" lastName="gates">






In order to solve this problem, some enthusiastic netizens, I wrote a jquery plug-in called jquery.xmlns.js. If you are interested, you can learn about and download it from the following
http://www.rfk.id.au/blog/entry/xmlns-selectors-jquery/
Then, we can use the following method to solve the problem
Copy the code 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); //Find all Employee nodes
var ul = $("
    ");
    employees.each(function () {
    $("
  • ").text($(this).attr("firstName") " " $(this).attr("lastName") ).appendTo(ul);
    });
    ul.appendTo(div);
    });

I have to say that the namespace in the XML technical specification is really a very bad design. It adds more trouble than it brings.
The complete code of the example in this article is as follows
Copy the code The code is as follows:

<% @ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>






< ;script language="javascript" type="text/javascript">
$(function () {
var div = $("#placeholder");
// Processing without namespace xml
$.get("data.xml", null, function (data) {
var employees = $("Employee", data); //Find all 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(div);
    });
    $("
    ").appendTo(div);
    $.xmlns["d"] = "http://tech .xizhang.com";
    // Process xml with namespace
    $.get("datawithnamespace.xml", null, function (data) {
    var employees = $("d|Employee" , data); //Find all Employee nodes
    var ul = $("
      ");
      employees.each(function () {
      $("
    • ").text($(this).attr("firstName") " " $(this).attr("lastName")).appendTo(ul);
      });
      ul.appendTo (div);
      });
      });










Finally, the effect seen in the browser is as follows. There are pictures and the truth

image

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve PHP error: The specified namespace class was not found Solve PHP error: The specified namespace class was not found Aug 18, 2023 pm 11:28 PM

Solve PHP error: The specified namespace class was not found. When developing using PHP, we often encounter various error messages. One of the common errors is "The specified namespace class was not found". This error is usually caused by the imported class file not being properly namespace referenced. This article explains how to solve this problem and provides some code examples. First, let’s take a look at an example of a common error message: Fatalerror:UncaughtError:C

Python implements XML data filtering and filtering Python implements XML data filtering and filtering Aug 09, 2023 am 10:13 AM

Python implements XML data filtering and filtering. XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. It is flexible and scalable and is often used for data exchange between different systems. When processing XML data, we often need to filter and filter it to extract the information we need. This article will introduce how to use Python to filter and filter XML data. Import the required modules Before starting, we

How to use namespace in F3 framework? How to use namespace in F3 framework? Jun 03, 2023 am 08:02 AM

The F3 framework is a simple, easy-to-use, flexible and scalable PHPWeb framework. Its namespace (Namespace) mechanism provides us with a more standardized, more readable, and clearer code structure. In this article, we will explore how to use namespaces in the F3 framework. 1. What is a namespace? Namespaces are often used to solve the problem of naming conflicts in PHP. It can encapsulate one or more classes, functions or constants in a namespace, which is equivalent to adding a prefix to them. example

Design ideas and implementation methods of Redis namespace and expiration mechanism Design ideas and implementation methods of Redis namespace and expiration mechanism May 11, 2023 am 10:40 AM

Redis is an open source, high-performance key-value storage database. When using Redis for data storage, we need to consider the design of the key namespace and expiration mechanism to maintain Redis performance and data integrity. This article will introduce the design ideas and implementation methods of Redis' namespace and expiration mechanism. 1. Redis namespace design ideas In Redis, keys can be set arbitrarily. In order to facilitate the management and distinction of different data types, Redis introduces the concept of namespace. Life

C++ syntax error: undefined namespace used, how to deal with it? C++ syntax error: undefined namespace used, how to deal with it? Aug 21, 2023 pm 09:49 PM

C++ is a widely used high-level programming language. It has high flexibility and scalability, but it also requires developers to strictly master its grammatical rules to avoid errors. One of the common errors is "use of undefined namespace". This article explains what this error means, why it occurs, and how to fix it. 1. What is the use of undefined namespace? In C++, namespaces are a way of organizing reusable code in order to keep it modular and readable. You can use namespaces to make functions with the same name

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Sep 11, 2023 pm 12:22 PM

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Introduction: PHP8 is an important version of the PHP programming language, which introduces many exciting new features and improvements. One of the most important new features is namespaces. Namespaces are a way to organize your code into a better structure that avoids conflicts between classes, functions, and constants with the same name. In this article, we’ll look at how to leverage namespaces and codes to better structure your PHP8 code

The Complete Guide: How to read and process XML data using the php extension SimpleXML The Complete Guide: How to read and process XML data using the php extension SimpleXML Jul 28, 2023 pm 02:46 PM

Complete Guide: How to Read and Process XML Data Using PHP Extension SimpleXML Introduction: In modern web development, processing and manipulating XML data is a very common task. As a powerful server-side scripting language, PHP provides a variety of extensions and functions for processing and manipulating XML data. Among them, the SimpleXML extension is a particularly useful tool that simplifies the process of reading and processing XML data. This article will provide you with a complete guide on how to use PHP extensions

Methods to solve PHP namespace errors and generate corresponding error prompts Methods to solve PHP namespace errors and generate corresponding error prompts Aug 07, 2023 pm 05:16 PM

How to resolve PHP namespace errors and generate corresponding error messages. PHP is a widely used server-side scripting language that is used to develop web applications. In PHP, namespace (Namespace) is a mechanism for managing and organizing code, which can avoid naming conflicts and improve code readability and maintainability. However, the complexity of namespace definition and use sometimes leads to errors. This article will introduce some methods to solve PHP namespace errors and generate corresponding error prompts. 1. Name space

See all articles