Introduction to XML

XML

XML (eXtensible Markup Language) is a meta-language built on SGML. XML can be used to describe data (for example database data) and can be also used for creating other markup languages. HTML was rewritten in XML and the result was XHTML.

With HTML tags, users can format or style pages. With XML users can only describe data. To make it easy for XML parsers to read XML data, users must be aware of uppercase and lowercase syntax. XML provided the background for technologies like RSS, OPML and SAML. XML applications contain typically the following:

  • The .xml file containing the XML data
  • The DTD (.dtd) or XML schema (.xsd) file that defines the structure of the main file.
  • A stylesheet file (optional).

Here is a very simple example to understand how XML works. Let’s say that we have to describe user account data. Our main file (.xml) will have the following code in it:

<?xml version="1.0" encoding="utf-8"?>
<store>
<account>
<username>Typpz</username>
<city>Athens</city>
<realname>Nikos</realname>
</account>

<account>
<username>Johnny</username>
<city>Thessaloniki</city>
<realname>John</realname>
</account>
</store>

For this code above the DTD (.dtd) file should contain the following elements to define the tags used.

<!ELEMENT store (account)*>
<!ELEMENT account (username, city, realname)>
<!ELEMENT username (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT realname (#PCDATA)>

After building your DTD file, save it as example.dtd (name the file as you want). Then you have to call the .dtd file from your .xml file. To do that, just place the followng line after <?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE store SYSTEM "example.dtd">

The main code will look like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE store SYSTEM "example.dtd">
<store>
<account>
<username>Typpz</username>
<city>Athens</city>
<realname>Nikos</realname>
</account>

<account>
<username>Johnny</username>
<city>Thessaloniki</city>
<realname>John</realname>
</account>
</store>

That was a very simple example for understanding XML structure. DTD files have many disadvantages, that can be solved using XML Schemas. These disadvantages and basic schema structure will be part of a following post.

Tagged on: , ,

2 thoughts on “Introduction to XML

  1. Pingback: Introduction to XML part 2

Leave a Reply

Your email address will not be published. Required fields are marked *