Quick tip: Leverage the power of jQuery to extract data from XML files-JS Tutorial-php.cn

Quick tip: Leverage the power of jQuery to extract data from XML files

王林
Release: 2023-09-04 18:01:07
Original
1082 people have browsed it

快速提示:利用 jQuery 的强大功能从 XML 文件中提取数据

In this quick tip, I'll show you how to load data from an XML file onto a blank page. We will use the $.get function and implement loading the gif when retrieving the information. We will present a simple list of recommended web development books. let's start.

Step 1: Check the script

First, let's look at a simple XML file. It only contains a few books with their titles, images, and descriptions.

    info goes here.     info goes here.     info goes here.   
Copy after login

Next, let's look at jQuery in action.

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

Recommended Web Development Books

'); $('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 = '
'; html += '
'; html += '

' + title + '

'; html += '

' + description + '

' ; html += '
'; $('dl').append($(html)); $('.loadingPic').fadeOut(1400); }); }); });
Copy after login

Step 2: Decipher the time

Since this is a quick tip, I'll run the script faster than usual. When the document is ready for manipulation, we will get the XML file using the "$.get" function. In parentheses we will pass in the location of the file and then run the callback function. I will use the variable "d" to represent the information extracted from the XML file. Next, we'll create a title tag and a definition list.

Continuing, we will search the XML file (d) and find the tag titled "book". Looking back at my files, there are three books in total. Therefore, we need to run the "each" method to perform operations on each book.Remember, "each" is like a "for" statement. This is a way to iterate over each element in a wrapped set.

In the next code block, I define some variables. To get the "title" and "description" from the XML file, we use ".attr(value)" - where "value" is equal to the attribute inside the tag.

Finally, we create a variable called "html" that will contain the html that displays the information from the XML file. However, just assigning that information to a variable will not display it on the page. To add it to the page, we get the list of definitions and append the variables. - $('dl').append($(html));

One more thing worth mentioning is that when retrieving information from the XML file we will display a standard loading gif (via a background image). Once the data is loaded, we'll grab the image and fade it out.

You’re done

I know I went through this very quickly; so please feel free to comment and ask any questions you may have.It should be noted that this script needs more work before it is ready for a real-world website. You have to compensate those who have turned off Javascript. In this case, if they do turn it off, they'll be staring at a blank page. You have to compensate for these things. But, I digress.

The above is the detailed content of Quick tip: Leverage the power of jQuery to extract data from XML files. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!