Apprentice XML

Begin your journey to the XML world with Root, Attributes, Namespaces and many more.

Posted by Rishi Raj Gujadhur 1/17/2016

Table of content




A. What is XML?


  • XML stands for eXtensible Markup Language.
  • XML is a tag-based syntax like HTML <h1> </h1>, however you can create your own tags.
  • XML is used to structure and describe information.

B. XML Trees

A simple XML tree has a root and an element.

Example of a simple XML document.

1. In Notepad ++, type:



<store> ... </store> ----> Root element
<product> ...</product> ----> Element within the root element

2. Save the document as Product.xml.

3. Run the document in the web browser. 


Lets at a slightly more complex XML tree.

1. Type:






<Food> ... </Food> ----> Root element; enclosing all other elements

<Fruits>...</Fruits> ----> Child of root element Food

<Fruit1> ... </Fruit1> ----> Subchild of the root element Food

<Fruit2> ... </Fruit2> ----> Subchild of the root element Food

2. Save as Food.xml

2. Run in the web browser.


Now lets look at a XML tree having attributes:
  • When coding in HTML we can add attributes to tags to style them. However in XML attributes are used to differentiate information as shown in the example below. 
  • Also XML attributes are often use as metadata (information describing other information); for example order IDs.

1. Type:


<Game category="PS4">

<Game category="Xbox One">
Froza Horizon 2

category="PS4" ----> attribute
category="Xbox One" ----> attribute

2. Save as Store.xml.

2. Run in the web browser.


C. Rules 

XML elements:

  • requires an openning and a closing tag <...> </...>
  • are case sentitive. <A>...</A> is different from <a>...</a>
  • requires proper nesting as show below.

Correct nesting

Incorrect nesting

XML attributes values requires quoting "PS4". <game category="PS4">...</game>

D. XML Comments

Creating XML comments is fairly simple as shown below.

<!-- Hello World -->
<!-- -----> Start of the comment.

Hello World ----> Comment

--> ----> End of the comment.

E. Namespaces

  • Conflicts occur when your using 2 XML document with similar tags. To avoid conflicts we use prefixes. In order to use a prefix in XML, we need to define a namespace for the prefix.
  • The xmlns attribute is utilize to define a prefix as shown below.

<f:table xmlns:f="FruitsURI">

<f: ----> Applying the prefix.

xmlns:f="Fruitsurl" ----> Defining a unique namespace; example a specific url for that prefix "f".

<v:table xmlns:v="Vegitablesurl">
xmlns:v="Vegitablesurl" ----> A different prefix used for a different kind of information.


F. Next Step

Feel free to practice all the concepts you learned in this tutorial using Notepad ++.