Implementation Code that jquery reads XML file data and displays

Source: Internet
Author: User

Preparations

Before starting, we need to make the following preparations:

1. Create a blank HTML file named demo.html. (editplus is recommended)

2. Be familiar with the basic syntax of the jquery framework. (It doesn't matter if you are not familiar with it. I will comment it out in detail later)

3. Create an XML file named data. XML to store data. The XML structure will be involved below. You can also download the packaged file for viewing;

4.a loading.gif image. This image is used to display in the blank HTML document during the waiting time when XML is read.

Official start

Step 1: Let's take a look at this data. the simple structure of XML. The data I demonstrated here is "Saturn recommends a few books for you". Therefore, for the book information, XML includes the name, thumbnail, and description of the book;

The following is an XML fileCode:Copy codeThe Code is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Books>
<Book title = "Tibetan password" imageurl = "images/tibet_code.jpg">
<Description>
Here is the overview (www.jb51.net)
</Description>
</Book>

<Book title = "Cambridge IELTS 6" imageurl = "images/ielts.jpg">
<Description>
Here is the overview (www.jb51.net)
</Description>
</Book>

<Book title = "professional ASP. NET" imageurl = "images/asp.jpg">
<Description>
Here is the overview (www.jb51.net)
</Description>
</Book>
</Books>

Next, let's look at the JavaScript code loaded in the blank HTML document:

Copy code The Code is as follows: $ (document). Ready (function ()
{
$. Get ('mydata. xml', function (d ){
$ ('Body'). append ('$ ('Body'). append ('<DL/> ');

$ (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 = '<DT> </DT> ';
HTML + = '<DD> <SPAN class = "loadingpic" alt = "loading"/> ';
HTML + = '<P class = "title">' + title + '</P> ';
HTML + = '<p>' + description + '</P> ';
HTML + = '</DD> ';

$ ('Dl '). append ($ (HTML ));

$ ('. Loadingpic'). fadeout (2000 );
});
});
});

Step 2: here, I will only talk about the principle and running process of JavaScript code, but I will discuss the syntax in detail. If you have any questions about the syntax, please leave a message or view jquery related documents.

Row 1: After the HTML document is prepared (both HTML and JavaScript are downloaded), the $ (document). Ready method of jquery and the process in it are automatically triggered. Obviously, the $. Get method is executed first.
Row 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 XML and webpage files in the same folder ). The second parameter is a callback function, that is, a callback function. That is to say, the get method is used to request the content of the XML file and then the callback function is used to operate the data in the file. The callback parameter D indicates all the data called back from XML. With this parameter D, we can proceed with the following content.
Row 4: dynamically Add a tag Row 5: A label <DL> is also dynamically added to the body to serve as the content container under the inclusion loop. (Row 20 will be used)
Row 7: This row is very important because we have already said that the callback function parameter D represents all the data called back from XML. Now we need to process (filter) the data) and format. Note: Here, we search for the book tag, and then cyclically execute the function following the each until the entries in the XML data are fully cyclic; (a bit like the foreach function in PHP)
Row 9: $ (this) actually creates an object to instantiate the current book information object of D for convenient operations. This is $ book;
Row 10 -- Row 12: Obtain the book name, description, and thumbnail of the current object $ book respectively. (Note that the syntax for retrieving attribute values and node values is different)
Line 14-line 18: format the book information for output;
Row 20: output formatted Information to the document in HTML output mode.
Row 22: To tell the user that the current information is being read from XML, the image disappears after 2000 milliseconds (2 seconds.

Step 3: Now, we are done. You are welcome to leave a message to discuss jquery development and your problems. In addition, place the downloaded folder in the web environment (IIS or virtual host), and do not open it directly.

Package and download code

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.