Chapter 5: XML instance parsing
outline:
1: Example effect
Two: Example Analysis 1. Define a new logo.
2. Create an XML document.
3. Create the corresponding HTML file.
XML is widely used in different fields, such as MathML in the technology field, WML in wireless communication applications, SVG in network images, etc. Here we focus on the application of XML on the web. The application of XML on the web mainly takes advantage of its powerful data manipulation capabilities. Generally using XML with server-side programs such as JavaScript and ASP, almost all application needs on the network can be realized.
Considering the convenience of explanation, we introduce a simple example below, which does not include the server-side program. The purpose is to give you a perceptual understanding of XML's data manipulation capabilities.
Okay, let’s first [click here] to see the effect of the example. (Please use IE5.0 or above version browser to open)
This is a simple CD record data retrieval function. You can see the relevant information of a single CD by clicking "Previous" and "Next". We originally used two methods to achieve this effect:
1. Use DHTML to hide data in different layers and display it sequentially through mouse events;
2. Use background programs (such as ASP, CGI, PHP, JSP, etc.) to call server-side data.
But in this example, when we open the original code of the page, we can see that there is no DHTML DIV or form action. It is completely implemented in XML. Let’s analyze its production process below:
Step one: Define a new identity.
According to the actual CD data, first create a new identification named <CD>; secondly, establish its related data identification, which are: CD name <Title>, singer <Artist>, publication year <Year>, country <Country> , the issuing company <Company> and the price <Price>; finally, a logo named catalog <CATALOG> must be created. Why create another <CATALOG> tag? Because it is stipulated in the XML document that there must and can only be one root element (identity), we have multiple CD data, and these data are in a parallel relationship, so we need to create a root element for these parallel elements.
The definitions and relationships of the above elements are fully compliant with XML standards and do not require a special DTD file to be defined, so the DTD definition can be omitted. If we want to use DTD to define, the above process can be expressed as:
<!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)>
This code indicates that the element CATALOG contains multiple CD sub-elements, and the sub-element CD in turn contains six sub-elements: Title, Artist, Year, Country, Company, Price, and their contents are defined as text (characters, numbers, text) . (Note: For specific syntax instructions, please see the introduction to DTD in the previous chapter)
Step 2: Create 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 first uses the <?xml version="1.0"?> declaration statement to indicate that this is an XML document, and its format complies with the XML 1.0 standard specification. Then there is the document content, the structure tree is very clear:
<CATALOG>
<CD>
...
</CD>
<CD>
...
</CD>
</CATALOG>
A total of 5 sets of data are defined. We save the above code as a cd.xml file for call.
Step 3: Create the corresponding HTML file.
1. Import XML data.
We know that among the currently popular browsers, only Microsoft's IE5.0 and above browsers currently support XML. IE supports inserting XML through the object object 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 with the ID name xmldso. Then use js to introduce xml data in the head area:
<script for="window" event="onload">
xmldso.XMLDocument.load("cd.xml");
</script>
2. Bundle data.
The <SPAN> tag will then be used to bind the XML data in the table. Among them, ID, DATASRC, and DTATFLD are all attributes of <SPAN>. The code is as follows:
<table>
<tr><td>Title:</td><td><SPAN ID="title" DATASRC=#xmldso DATAFLD="TITLE"></SPAN></td></tr>
<tr><td>Artist:</td><td><SPAN ID="artist" DATASRC=#xmldso DATAFLD="ARTIST"></SPAN></td></tr>
<tr><td>Year:</td><td><SPAN ID="year" DATASRC=#xmldso DATAFLD="YEAR"></SPAN></td></tr>
<tr><td>Country:</td><td><SPAN ID="country" DATASRC=#xmldso DATAFLD="COUNTRY"></SPAN></td></tr>
<tr><td>Company:</td><td><SPAN ID="company" DATASRC=#xmldso DATAFLD="COMPANY"></SPAN></td></tr>
<tr><td>Price:</td><td><SPAN ID="price" DATASRC=#xmldso DATAFLD="PRICE"></SPAN></td></tr>
</table>
3. Action operation.
Finally, provide browse buttons 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 click functions: 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>
Okay, let’s look at the entire original code of the HTML file first:
<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><td><SPAN ID="title" DATASRC=#xmldso DATAFLD="TITLE"></SPAN></td></tr>
<tr><td>Artist:</td><td><SPAN ID="artist" DATASRC=#xmldso DATAFLD="ARTIST"></SPAN></td></tr>
<tr><td>Year:</td><td><SPAN ID="year" DATASRC=#xmldso DATAFLD="YEAR"></SPAN></td></tr>
<tr><td>Country:</td><td><SPAN ID="country" DATASRC=#xmldso DATAFLD="COUNTRY"></SPAN></td></tr>
<tr><td>Company:</td><td><SPAN ID="company" DATASRC=#xmldso DATAFLD="COMPANY"></SPAN></td></tr>
<tr><td>Price:</td><td><SPAN ID="price" DATASRC=#xmldso DATAFLD="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 above code as a cd.htm file and put it together with the cd.xml file in the second step. Open the cd.htm file and you will see the same effect as the example above.
Okay, so far, we have learned a lot about XML. Let's summarize the previous five chapters, which are XML quick introduction, XML conceptual principles, XML terminology, XML syntax and example analysis of this chapter. Here, the tutorial part ends. During the writing process, Ajie tried his best to explain the relevant XML concepts in an easy-to-understand manner, and tried his best to tell everyone his understanding. However, because I have not studied XML for a long time, my grasp of the entire XML technology is not systematic and in-depth enough. , so it is inevitable that there are some omissions. Please correct me and forgive me. Thank you!