Home Web Front-end JS Tutorial JQuery reads XML file data and displays the implementation code_jquery

JQuery reads XML file data and displays the implementation code_jquery

May 16, 2016 pm 06:39 PM
jquery xml

Preparation work

Before we start, we need to do the following preparation work:

1. Create a blank html file named DEMO.html; (it is recommended to use Editplus to create it)

2 .Be familiar with the basic syntax of the JQuery framework; (It doesn’t matter if you are not familiar with it, I will explain it in detail later)

3. Create an XML file named data.xml to store data. The structure of XML will be covered below. Yes, you can also download the file I packaged to view;

4. A loading.gif image, this image is used to display in the blank html document during the waiting time for reading the XML

Official start

Step 1: First, let us take a look at the simple structure of this data.xml. The data I demonstrate here is "Several books recommended by Saturn for you", so it is book information, then xml includes the name, thumbnail and book description information of the book;

The following is the XML file code:

Copy code The code is as follows:





Here is the overview (www.jb51.net)





Here is the overview (www.jb51.net)





Here is the overview (www.jb51.net)






Secondly, let’s look at the JavaScript code loaded in a blank HTML document:

Copy the code The code is as follows:

$(document).ready(function()
{
$.get('myData.xml', function(d){
$('body').append ('

Saturn recommends some books to you:

');
$('body').append('
');

$(d).find('book').each(function(){

var $book = $(this);
var title = $book.attr("title");
var description = $book.find('description').text();
var imageurl = $book.attr('imageurl');

var html = '
< ;img class="bookImage" alt="" src="' imageurl '" />
';
html = '
';
html = '

' title '

';
html = '

' description '

' ;
html = '
';

$('dl').append($(html));

$('.loadingPic'). fadeOut(2000);
});
});
});


Step 2: Here, I will only talk about the principles and operation process of JavaScript code, but will not discuss the syntax too much. If you have any questions about the syntax, please leave me a message or check out the JQuery related documents.

Line 1: When the HTML document is prepared (that is, both html and JavaScript are downloaded), JQuery's $(document).ready method and the process inside will be automatically triggered. Obviously, the $.get method is executed first here.
Line 3: The first parameter of $.get is the relative path of the XML file (note that the path must be filled in correctly. Here we put the XML and web page files in the same folder). The second parameter is a Callback function, that is, the callback function. That is to say, the content of this XML file is requested through the get method, and then the data inside is manipulated through this callback function. The parameter d of callback represents all the data returned from the XML callback. With this parameter d, we can proceed with the following content.
Line 4: Dynamically add a tag

in the BODY of the document through JavaScript. This is the overall title of the page, it doesn’t matter;
Line 5: Also dynamically add a tag
in the BODY , used as a content container under the containing loop. (Line 20 will be used)
Line 7: This line is important because we have already said that the parameter d of the callback function represents all the data callback from XML. Now we need to process (filter) these data and Formatting; please note: here, the book tag (tag) is searched, and then the function after each is executed in a loop until the data entries in the xml are completely cycled; (a bit like the function of the foreach function in PHP)
Line 9: $(this) actually creates an object. The purpose is to instantiate the current book information object of d to facilitate operation. This is $book;
Line 10--Line 12: Get the current object $book respectively. Book name, description and thumbnail; (note that the syntax for getting attribute values ​​and node values ​​is different)
Line 14-Line 18: Format book information for output;
Line 20: Format the information Output to the document in HTML output mode.
Line 22: To tell the user that our current information is being read from XML, the image fades away after 2000 milliseconds (2 seconds).

Step 3: At this point, you’re done. Everyone is welcome to leave me a message to discuss the development of JQuery and the problems you encounter. Please feel free to give me advice. In addition, please run the downloaded folder in a WEB environment (IIS or virtual host). Please do not click to run it directly.

Code package download

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

Video Face Swap

Video Face Swap

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

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)

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

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: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

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:

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Comparison of Java libraries for XML parsing: Finding the best solution Comparison of Java libraries for XML parsing: Finding the best solution Mar 09, 2024 am 09:10 AM

Introduction XML (Extensible Markup Language) is a popular format for storing and transmitting data. Parsing XML in Java is a necessary task for many applications, from data exchange to document processing. To parse XML efficiently, developers can use various Java libraries. This article will compare some of the most popular XML parsing libraries, focusing on their features, functionality, and performance to help developers make an informed choice. DOM (Document Object Model) parsing library JavaXMLDOMAPI: a standard DOM implementation provided by Oracle. It provides an object model that allows developers to access and manipulate XML documents. DocumentBuilderFactoryfactory=D

See all articles