XML easy learning Manual (5): XML instance Parsing

Source: Internet
Author: User
XML easy learning Manual (5): XML instance Parsing

SubmittedJiashixiangOn 2006, July 26, am. xml

Chapter 5: XML instance Parsing

Outline:
I. instance Effect
Ii. instance resolution

1. define a new identifier.
2. Create an XML document.
3. Create an HTML file.

XML is widely used in different fields, such as MathML in the field of science and technology, WML in wireless communication, and SVG in network image, here we will focus on the application of XML on the web. XML applications on the Web mainly use its powerful data operation capabilities. Generally, XML can be used with JavaScript, ASP, and other server programs to meet almost all application requirements on the network.

For convenience, we will introduce a simple example below, which does not contain server programs. The purpose is to give you a perceptual knowledge of XML data operation capabilities.

Well, first [click here] to see the effect of the instance. (Open it in ie5.0 or a later browser)

This is a simple data retrieval function for CD records. Click "previous" and "Next" to view information about a single CD. We can achieve this in two ways:

1. Use DHTML to hide data in different layers and display data by mouse events in sequence;
2. Use background programs (such as ASP, CGI, PHP, JSP, etc.) to call server data.

However, in this example, we can see that the original code of the page does not use DHTML Div or form action, and it is fully implemented using XML. Next we will analyze its production process:

Step 1: define a new identifier. Based on the actual CD data, first create a new identifier named <Cd>, and then create its related data identifiers: CD name <title>, artist <artist>, <year>, country <country>, issuer <company>, and price <price> In the publication period. Finally, you must create a directory named <catalog>. Why do we need to create another <catalog> identifier? Because the XML document stipulates that there must be only one root element (identifier), and we have multiple CD data, which is a parallel relationship, therefore, you need to create a root element for these columns.
The definitions and relationships of the above elements fully comply with the XML standard and do not need to be defined by a special DTD file. Therefore, you can omit the DTD definition. If we want to use DTD to define the process, the above process can be expressed:

<! Element catalog (CD) *>
<! Element Cd (title, artist, year, country, company, price)>
<! Element title (# pcdata)>
<! Element artist (# pcdata)>
<! Element year (# pcdata)>
<! Element country (# pcdata)>
<! Element Company (# pcdata)>
<! Element price (# pcdata)>

The Code indicates that the element catalog contains multiple CD sub-elements, and the sub-element CD contains six sub-elements in sequence: title, artist, year, country, company, and price, their content is defined as text (characters, numbers, text ). (Note: For detailed syntax descriptions, refer to the introduction to DTD in the previous chapter)

Step 2: Create an XML document.

<? XML version = "1.0"?>
<Catalog>
<Cd>
<Title> empire burlesque </title>
<Artist> Bob Dylan </artist>
<Country> USA </country>
<Company> Columbia </company>
<Price> 10.90 </price>
<Year> 1985 </year>
</Cd>
<Cd>
<Title> hide your heart </title>
<Artist> Bonnie Tylor </artist>
<Country> UK </country>
<Company> CBS Records </company>
<Price> 9.90 </price>
<Year> 1988 </year>
</Cd>
<Cd>
<Title> Greatest Hits </title>
<Artist> Dolly Parton </artist>
<Country> USA </country>
<Company> RCA </company>
<Price> 9.90 </price>
<Year> 1982 </year>
</Cd>
<Cd>
<Title> still got the blues </title>
<Artist> Gary more </artist>
<Country> UK </country>
<Company> virgin redords </company>
<Price> 10.20 </price>
<Year> 1990 </year>
</Cd>
<Cd>
<Title> Eros </title>
<Artist> Eros ramazzotti </artist>
<Country> Eu </country>
<Company> BMG </company>
<Price> 9.90 </price>
<Year> 1997 </year>
</Cd>
</CATALOG>

The above Code uses <? XML version = "1.0"?> The declaration statement indicates that this is an XML document and its format complies with the XML 1.0 standard. Then there is the document content, and the structure tree is very clear:
<Catalog>
<Cd>
......
</Cd>
<Cd>
......
</Cd>

</CATALOG>
A total of five groups of data are defined. We save the above Code as a CD. xml file for calling.

Step 3: Create an HTML file.
1. Import XML data.
We know that currently, only Microsoft ie5.0 and later browsers support xml. IE supports XML insertion through object objects in HTML and imports data through the xmldocument. Load () method of Js. Let's look at the Code:

<Object width = "0" Height = "0"
Classid = "CLSID: 550dda30-0541-11d2-9ca9-0060b0ec3d39" id = "xmldso">
</Object>

Define an object named xmldso. Then introduce XML data with Js in the head area:

<Script for = "window" event = "onLoad">
Xmldso. xmldocument. Load ("CD. xml ");
</SCRIPT>

2. Bind data.
Then, the XML data is bound to the table with the <span> identifier. Where ID, datasrc, and dtatfld are all <span> attributes. The Code is as follows:

<Table>
<Tr> <TD> title: </TD> <span id = "title" datasrc = # xmldso dataworks = "title"> </span> </TD> </tr>
<Tr> <TD> Artist: </TD> <span id = "artist" datasrc = # xmldso dataworks = "artist"> </span> </TD> </tr>
<Tr> <TD> year: </TD> <span id = "year" datasrc = # xmldso data== "year"> </span> </TD> </tr>
<Tr> <TD> country: </TD> <span id = "country" datasrc = # xmldso data== "country"> </span> </TD> </tr>
<Tr> <TD> company: </TD> <span id = "company" datasrc = # xmldso dataworks = "company"> </span> </TD> </tr>
<Tr> <TD> price: </TD> <span id = "price" datasrc = # xmldso dataworks = "price"> </span> </TD> </tr>
</Table>

3. Action operations.
Finally, provide the browsing button for the data:
<Input type = button value = "previous CD" onclick = "moveprevious ()">
<Input type = button value = "next CD" onclick = "movenext ()">

And use js to complete two mouse clicks: movenext () and moveprevious (). Add the following code to the head area:

<Script language = "JavaScript">
Function movenext ()
{
If (xmldso. recordset. absoluteposition <xmldso. recordset. recordcount)
{
Xmldso. recordset. movenext ();
}
}
Function moveprevious ()
{
If (xmldso. recordset. absoluteposition> 1)
{
Xmldso. recordset. moveprevious ();
}
}
</SCRIPT>

Well, let's first look at all the original code of the HTML file:

<HTML>
<Head>

<Script for = "window" event = "onLoad">
Xmldso. xmldocument. Load ("CD. xml ");
</SCRIPT>

<Script language = "JavaScript">
Function movenext ()
{
If (xmldso. recordset. absoluteposition <xmldso. recordset. recordcount)
{
Xmldso. recordset. movenext ();
}
}
Function moveprevious ()
{
If (xmldso. recordset. absoluteposition> 1)
{
Xmldso. recordset. moveprevious ();
}
}
</SCRIPT>

<Title> Cd navigate </title>
</Head>

<Body>
<P>
<Object width = "0" Height = "0"
Classid = "CLSID: 550dda30-0541-11d2-9ca9-0060b0ec3d39" id = "xmldso">
</Object>

<Table>
<Tr> <TD> title: </TD> <span id = "title" datasrc = # xmldso dataworks = "title"> </span> </TD> </tr>
<Tr> <TD> Artist: </TD> <span id = "artist" datasrc = # xmldso dataworks = "artist"> </span> </TD> </tr>
<Tr> <TD> year: </TD> <span id = "year" datasrc = # xmldso data== "year"> </span> </TD> </tr>
<Tr> <TD> country: </TD> <span id = "country" datasrc = # xmldso data== "country"> </span> </TD> </tr>
<Tr> <TD> company: </TD> <span id = "company" datasrc = # xmldso dataworks = "company"> </span> </TD> </tr>
<Tr> <TD> price: </TD> <span id = "price" datasrc = # xmldso dataworks = "price"> </span> </TD> </tr>
</Table>

<P>
<Input type = button value = "previous CD" onclick = "moveprevious ()">
<Input type = button value = "next CD" onclick = "movenext ()">
</P>

</Body>
</Html>

Save the code as a cd.htm file, and put the CD. xml file in step 2 together. Open the cd.htm file and you will see the same effect as the above instance.

Well, we have learned a lot about XML so far. Let's summarize the first five chapters, namely, XML Quick Start, XML concept principle, and XML terminology, XML syntax and instance parsing in this chapter. At this point, the tutorial is complete. In the process of writing, Jack tried his best to make the XML concepts easy to understand and try to give his understanding to everyone, but it was not long for me to learn XML, we do not have enough system or in-depth understanding of the XML technology, so we will inevitably find some omissions. Please correct and understand. Thank you!

 

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.