Creating and Displaying a Simple XML Document

Following is a sample XML document. The only required predefined tag is on the first line, which describes that the version of the XML parser used is version 1.0:

   <?xmlversion="1.0"?> <WeatherForecast date="2/1/2004">    <city>       <name>Frankfurt</name>       <temperature>       <min>43</min>          <max>52</max>       </temperature>    </city>    <city>       <name>London</name>       <temperature>          <min>31</min>          <max>45</max>       </temperature>    </city>    <city>       <name>Paris</name>       <temperature>          <min>620</min>          <max>74</max>       </temperature>    </city> </WeatherForecast>   

A parser is a program that analyzes and verifies the syntax of the coding of a programming language. An XML- capable browser parses XML code to ensure that is syntactically correct. As already mentioned, one parser function is to ensure that all starting and ending tags exist, and that there is no interlocking of XML tags within the document. Interlocking implies that a new tag of the same type, such as <city> , cannot be started, until the ending tag of the previous city ( </city> ), has been found.

In a browser, the XML document looks as shown in Figure 1-3. The callouts in Figure 1-3 show that in addition to being flexible for a web pages programmer, XML is even flexible to the end user . End users are unlikely to see an XML document in this raw state, but Figure 1-3 helps to demonstrate the flexibility of XML.

image from book
Figure 1-3: A simple sample XML page

The primary purpose of HTML is for display of data. XML is intended to describe data. XML is the data and thus describes itself. When HTML pages contain data, they must be explicitly generated. For every web page weather report written in HTML, a new HTML page must be created. This includes both the weather report data and all HTML tags. When regenerating an XML-based weather report, only the data is regenerated. Any templates using something like XSL remain the same. And those templates are probably only downloaded once. The result is that XML occupies less network bandwidth and involves less processing power.

XML is also a very capable medium for bulk data transfers that are platform and database independent. This is because XML is a universal standard. In short, XML does not do as much processing as HTML does. XML is structure applied to data. Effectively XML complements HTML rather than replaces it. XML was built to store and exchange data; HTML is designed to display data. XSL, on the other hand, is designed to format data.

Try It OutCreating a Simple XML Document
image from book

The data shown below represents three regions containing six countries :

   Africa           Zambia Africa           Zimbabwe Asia             Burma Australasia      Australia Caribbean        Bahamas Caribbean        Barbados   

Here, you are going to create a single hierarchy XML document. The example shown in Figure 1-3, and its preceding matching XML data, gives you an example to base this task on.

Create the XML document as follows :

  1. Use an appropriate editor to create the XML document text file (Notepad in Windows).

  2. Create the XML tag:

       <?xml version="1.0"?>   
  3. Create the root tag first. The data is divided up as countries listed within continents (regions). Countries are contained within regions. There are multiple regions so there has to be a tag, which is a parent tag of the multiple regions. If there was a single region there could be a single <region> tag as the root node. So create a root node such as <regions> , indicating multiple regions. The XML document now looks something like this:

       <?xml version="1.0"?> <regions> </regions>   
  4. Now add each region in as a child of the <regions> tag. It should look something like this:

       <?xml version="1.0"?> <regions>    <region>Africa</region>    <region>Asia</region>    <region>Australasia</region>    <region>Caribbean</region> </regions>   
  5. Next you can add the individual countries into their respective regions by creating individual <country> tags:

       <?xml version="1.0"?> <regions>    <region>Africa</region>       <country>Zambia</country>       <country>Zimbabwe</country>    <region>Asia</region>       <country>Burma</country>    <region>Australasia</region>       <country>Australia</country>    <region>Caribbean</region>       <country>Bahamas</country>       <country>Barbados</country> </regions>   
  6. When executed in a browser, the result will look as shown in Figure 1-4.

image from book
Figure 1-4: Creating a simple XML document

How It Works

You opened a text editor and created an XML document file. The XML document begins with the XML tag, identifying the version of XML is use. Next you added the root node called <regions> . All XML documents must have a single root node. Next you added four <region> nodes representing four regions into the root node. Next you added countries into the four different regions. Last, you viewed the XML document in your browser.

image from book
 

Embedding XML in HTML Pages (Data Islands)

XML documents can also be displayed in a browser using an XML data island. An XML data island is an XML document (with its data) directly or indirectly embedded inside an HTML page. An XML document can be embedded inline inside an HTML page using the HTML <XML> tag. It can also be referenced with an HTML SRC attribute.

This first example uses the XML tag to embed XML document data within an HTML page:

   <HTML><BODY> <XML ID="xmlParts">    <?xml version="1.0" ?>    <parts>       <part>          <partnumber>X12334-125</partnumber>          <description>Oil Filter</description>          <quantity>.99</quantity>       </part>       <part>          <partnumber>X44562-001</partnumber>          <description>Brake Hose</description>          <quantity>.45</quantity>       </part>       <part>          <partnumber>Y00023-12A</partnumber>          <description>Transmission</description>          <quantity>00.00</quantity>       </part>    </parts> </XML> <TABLE DATASRC=#xmlParts> <TR>    <TD><DIV DATAFLD="partnumber"></DIV></TD>    <TD><DIV DATAFLD="$text"></DIV></TD> </TR> </TABLE> </BODY></HTML>   

HTML and XML tags can have attributes or descriptive values. In the HTML code <IMG src="image.jpg" BORDER="1"> the tag is an <IMG> or image tag for referencing an image. The SRC attribute tells the HTML <IMG> tag where to find the image, and the BORDER tag tells HTML to put a 1 pixel wide border around the image.

The second example allows a reference to a separate XML file using the SRC attribute of the XML tag.

The XML source file is stored externally to the HTML page. In this case, the parts.xml file is stored in the operating system and not stored within the HTML file as in the previous example:

   <HTML><BODY> <XML ID="xmlParts" SRC="parts.xml"></XML> <TABLE DATASRC=#xmlParts>    <TR><TD><DIV DATAFLD="partnumber"></DIV></TD>       <TD><DIV DATAFLD="$text"></DIV></TD>    </TR> </TABLE> </BODY></HTML>   

Both of these examples look as the screen does in Figure 1-5.

image from book
Figure 1-5: Using the XML tag to embed XML data islands into an HTML page

There are always different ways to do things.

Try It OutXML Data Islands
image from book

The XML document that follows represents the three regions and six countries created in the Try It Out exercise presented earlier in this chapter:

   <?xml version="1.0"?> <regions>    <region>Africa</region>       <country>Zambia</country>       <country>Zimbabwe</country>    <region>Asia</region>       <country>Burma</country>    <region>Australasia</region>       <country>Australia</country>    <region>Caribbean</region>       <country>Bahamas</country>       <country>Barbados</country> </regions>   

Here we will create a simple HTML page, containing the preceding XML document as a data island. Assume that the XML document is called countries.xml. Dont worry about a full path name. The example shown in Figure 1-5 and its preceding matching XML data island HTML pages give you an example to base this task on.

Create the HTML page as follows:

  1. Use an appropriate editor to create a text file.

  2. Begin by creating the <HTML> tags for the start and end of the HTML page:

       <HTML> </HTML>   
  3. You could add a <HEAD> tag, allowing inclusion of a title into the browser. Begin by creating the <HTML> tags for the start and end of the HTML page:

       <HTML> <HEAD><TITLE>Regions and Countries</TITLE></HEAD> </HTML>   
  4. Add the body section for the HTML page by enclosing it between the <BODY> tags:

       <HTML> <HEAD><TITLE>Regions and Countries</TITLE></HEAD> <BODY> </BODY> </HTML>   
  5. Now add the <XML> tag into the body of the HTML page, which references the externally stored XML document:

       <HTML> <HEAD><TITLE>Regions and Countries</TITLE></HEAD> <BODY> <XML ID="xmlCountries" SRC="countries.xml"></XML> </BODY> </HTML>   
  6. Add a table field ( <TABLE> tag) to the HTML page. The table field references the <XML> tag, by the ID attribute, as shown in the code that follows. The SRC in the <XML> tag allows direct access from the HTML page to XML tags as stored in the countries.xml file. In other words, the countries.xml file is referenced from the HTML page as a referenced data island:

       <HTML> <HEAD><TITLE>Regions and Countries</TITLE></HEAD> <BODY> <XML ID="xmlCountries" SRC="countries.xml"></XML> <TABLE DATASRC=#xmlCountries> <TR>    <TD><DIV DATAFLD="region"></DIV></TD>    <TD><DIV DATAFLD="$text"></DIV></TD> </TR> </TABLE> </BODY> </HTML>   
  7. The result will look as shown in Figure 1-6, when executed in a browser.

    image from book
    Figure 1-6: Creating a simple HTML page containing an XML data island

How It Works

You created an HTML page that referenced an XML document from the HTML page as a data island. The data island is referenced from the HTML page, to the XML document, using the XML tag as defined in the HTML page. Data is scrolled through in the HTML page using an HTML table field, using the DATASRC attribute of the HTML <TABLE> tag.

image from book
 


Beginning XML Databases
Beginning XML Databases (Wrox Beginning Guides)
ISBN: 0471791202
EAN: 2147483647
Year: 2006
Pages: 183
Authors: Gavin Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net