Benefits of XML

An XML document is a plain text file, and you can use any text editor to write, view, and edit it. When you see an XML document for the first time, you will immediately notice that some words are enclosed in brackets: < ... >. The words in brackets are called tags. If you already know Hypertext Markup Language (HTML), you’ll feel at home learning XML. However, unlike HTML, there are no predefined tags in XML. Every tag is tailor-made—in other words, you create your own!

The main use of XML is to store data, just like relational databases do. If relational databases work so well, why do you need XML at all? XML has the following advantages over relational databases.

  • XML uses a standard and open format from the W3C (www.w3c.org), making data exchange easy. Compare this with proprietary data formats of relational databases that make exchanging data a complex process.

  • XML can represent hierarchical data.

  • You must comply with rules when writing XML. These rules make it possible to check the data integrity of an XML document.

  • The same rules make it possible to validate XML documents.

  • XML is extensible. You create your custom tags to accommodate any type of data you have.

Note

You can find the XML recommendation version 1.0 (the latest version) at http://www.w3.org/TR/REC-xml.


Consider, for example, a table containing product information as given in Table A.1.

Table A.1. A Products table
ProductIdNameDescriptionPriceSupplierId
1ChocChicMint chocolate 100g1.501
2ChocnutsChocolate with peanuts, 200g2.952

If the two records in Table A.1 is to be written in XML, you will get the XML document in Listing A.1.

Listing A.1. An XML document that contains product information
<?xml version="1.0" standalone="yes" encoding="UTF-8"?>
<products>
     <product>
         <product_id>1</product_id>
         <name>ChocChic</name>
         <description>Mint Chocolate 100g</description>
         <price>1.50</price>
         <supplier_id>1</supplier_id>
     </product>
     <product>
         <product_id>2</product_id>
         <name>Chocnuts</name>
         <description>Chocolate with peanuts, 200gr</description>
         <price>2.95</price>
         <supplier_id>2</supplier_id>
    </product>
</products>

The first line tells you that it is an XML document that is compliant with version 1.0, it is a stand-alone document (meaning it does not refer to any external entity), and it uses UTF-8 encoding.

The second line begins the data, which is represented as a collection of elements. The first element in the XML document is products, which indicates a collection of products. The first element is called the root. So, <products> is the root of the XML document in Listing A.1.

An element can contain other elements, which are called the child elements of the first element. As previously mentioned, XML is suitable for hierarchical data. Therefore, elements with child elements with child elements are common in XML. For example, the product element is the child element of the products element. The product element has the child elements product_id, name, description, price, and supplier_id.

An element in an XML document can contain attributes, which are similar to attributes in an HTML tag. For example, the following is a product element to which an in_stock attribute has been added:

<product in_stock="yes">

Note

Element names and attribute names are case sensitive.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset