g
Printer Friendly Version

editor  
BellaOnline's HTML Editor
 

Building an XML File

If you’re not already familiar with the basic rules of XML, I suggest you start by reading the "XML Basics" article.

An XML file is laid out somewhat differently than an HTML file. With HTML, each page has a head section (with information for the browser on how to display the page) and a body section (the part that lays out code for what the visitor actually sees). XML pages, on the other hand, are made up of nesting elements. Each element consists of an opening and a closing tag, possibly with text or other elements in between.

The only part of an XML document that does not conform to this rule is the very first line, called the XML Prolog. It will look something like this:

<?xml version="1.0" encoding="ISO-8859-15"?>

You may notice that the prolog is similar in format to HTML’s Document Type Declaration, which appears at the beginning of every HTML page. The prolog serves a similar purpose; the first section announces what XML version you used to design your file, and the second section references the file’s character set – in the above example, we’re using the standard Latin alphabet.

Let’s say you are a realtor, and you've decided to build a website that shows your current inventory of houses available for sale. Your XML data file would look something like this:

<?xml version="1.0" encoding="ISO-8859-15"?>
<forsale>

<house id="301">
<address>123 Easy Street</address>
<city>Beverly Hills</city>
<bedrooms>3</bedrooms>
<baths>2</baths>
<price>200,000</price>
<comments>What a bargain!</comments>
</house>

<house id="302">
<address>84 Towering Lane</address>
<city>Malibu</city>
<bedrooms>4</bedrooms>
<baths>2</baths>
<price>400,000</price>
<comments>Ocean view</comments>
</house>

</forsale>

In the above example, the outside element <forsale></forsale> is the document's root element. Every XML document must have one and only one root element, which is the parent for all the other elements in the document. Naturally, an element inside another element is called a child element.

A child element must be nested inside its parent – and it cannot overlap a sibling element's tags. Here is an example of how NOT to build elements:

<house id="301">
<address>
<city>
</address>
</city>
</house>


As with HTML, XML elements can have attributes, which label or define the element. In the case of our house listing database, we used the attribute 'id' in the <house> element. Attributes are completely customizable.

Another helpful tool in XML is the entity. An entity is a piece of text that you can use as shorthand for something else. HTML has entities too, although unlike XML you can't define your own – you can only use the predefined ones. Some examples of entities in HTML are '–' (to insert an n-dash) or '©' (to insert the copyright symbol).

Entities in XML are defined at the beginning of the document and can then be used within any element in that document. To return to the realty database example, let's say we wanted to include the text 'This house is available for immediate viewing. Call us to schedule an appointment.' for some of our listed houses. Rather than type it out for every available house, we can create an entity called 'open' to simplify the process:

<?xml version="1.0" encoding="ISO-8859-15"?>
<!ENTITY open "This house is available for immediate viewing. Call us to schedule an appointment.">

We can then call on this entity within the document. Let's say we decide to put it in the <comments> text:

<house id="301">
<address>123 Easy Street</address>
<city>Beverly Hills</city>
<bedrooms>3</bedrooms>
<baths>2</baths>
<price>200,000</price>
<comments>What a bargain! &open;</comments>
</house>

Congratulations, you can now design and build your own XML files. Next time we'll discuss how to incorporate XML into your HTML pages.

This site needs an editor - click to learn more!

HTML Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Elizabeth Connick. All rights reserved.
This content was written by Elizabeth Connick. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor