jQuery traversing XML nodes and attributes implementation steps
This time I will bring you the steps to implement jQuery for traversing XML nodes and attributes. What are the precautions for jQuery for traversing XML nodes and attributes? The following is a practical case, let’s take a look.
After some research, it seems that jquery does not have attributes. We still need to rely on the original JS
The following is the JS code
<script type="text/javascript" src="js/jquery.min.js"></script> <p id="c"></p> <script> //用于缩进, 根据节点级别 var M_DOT = 1; //递归函数, 用于遍历XML, 同时打印出来 function fn(obj){ //定义一个变量, 用于缩进 var dotString = ""; //根据缩进量, 得到缩进的长度 for(j = 1; j <= M_DOT; j++){ dotString += " "; } //jquery不支持attributes属性集, 转为原生js, 并赋给myObj变量 var myObj = obj[0].attributes; //打印缩进 $("#c").append(dotString) //打印属性集nodeName, nodeValue为原生JS, 分别表示为属性的名称, 和属性值 $(myObj).each(function(i){ $("#c").append( ($(this)[0].nodeName) + "=<u>" + ($(this)[0].nodeValue) + "</u> " ); }) //打印节点的文本 //$("#c").append( "<b>" + obj.text() + "</b><br/>"); //打印换行 $("#c").append( "<br/>"); //判断DOM有无子DOM if( obj.length > 0 ){ //如果有, 遍历之 obj.children().each(function(i){ //缩进量加一 M_DOT++; //递归遍历子DOM fn($(this)); //缩进量加一 M_DOT--; }) }else{ //如果没有子DOM, 返回false return false; } } //程序入口 $(document).ready(function() { //ajax获取xml数据, 详细用法见jquery手册 $.get('config.xml', function(d){ //len = $(d).find("*").length; //找到xml顶级结点. ('*:first'):就是查找第一个DOM. 详细说明见jquery手册 //find, children(), each, 以及后面用到的 append 等请查阅jquery手册 $(d).find('*:first').children().each(function(i){ //找到顶级结点的子结点, 并把子结点对象传给fn函数 fn($(this)); }); }); }); </script>
Attached is an XML file
Please name config.xml and put it in the same directory as the above js, and put them both in the site
<?xml version="1.0" encoding="utf-8" ?> <roster> <student ID="s101"> <name>李华</name> <sex>男</sex> <birthday>1978.9.12</birthday> <score>92</score> <skill>Java</skill> <skill>Oracle</skill> <skill>C Sharp</skill> <skill>SQL Server</skill> </student> <student ID="s107"> <name>李寻欢</name> <sex>男</sex> <birthday>1981.4.19</birthday> <score>58</score> <skill>UML</skill> <skill>C Sharp</skill> <skill>XML</skill> <skill>SQL Server</skill> </student> </roster>
I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website!
Recommended reading:
Detailed explanation of the steps to implement full-screen scrolling plug-in in ES6
##vue vue-router vuex operation permissions
The above is the detailed content of jQuery traversing XML nodes and attributes implementation steps. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece "Hearthstone" and has a chance to be obtained in the "Wizbane's Workshop" card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions
