XML Tutorial

Understanding XML Structure


XML Structure
Structure


XML has XML Definition, below it Root element and inside it its child elements and attribute.


To write a correct XML we need flow few guidelines:

XML Syntax:

<XML Definition>
<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>
1. XML Definition:


This is an optional field, but used in most of the XML. This field comes at the very start of your XML and should not even have any space or character before it. This defines the metadata for the XML been used below. It contains XML version and XML file encoding.

XML file Encoding – File encoding depicts various characters a particular file may have. Every encoding have different sets of characters build into it. For example: If you live in US for example, you could go pretty far with ASCII. But in many counties we need characters like ä, å, ü etc. (If so was ASCII only or you try to read this text as ASCII encoded text, you’d see some weird characters in the places of ä, å and ü.) Think also the China, Japan, Thailand and other “exotic” countries, you might need more verity of characters thus need to use a different encoding type.
There are various different types of file encoding, example: UTF-8 (most commonly used), UTF-16, UTF-32, ISO-8859 etc.

<?xml version="1.0" encoding="UTF-8"?>
2. Root Element


This is a mandatory field, and is the parent of all the elements. All the other elements to be defined in XML should and must come inside one root element. There can only be one root element in the XML. Here “student” is the root element and also the parent element to its child element “firstname” and “lastname

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <firstname>Varun</firstname>
    <lastname>Goel</lastname>
</student>
3. Child Elements


All the other elements defined comes inside root element. Which can have other elements or attributes in it.

XML has 2 basic components


  • Elements
  • Attribute
Elements


Elements
Elements are building blocks of XML. Element are defined using tags. There are closing and opening tag in every element in XML.

Opening Tag:

<firstname>

Closing Tag:

</firstname>

Both opening and closing tags when combined together creates element

<firstname></firstname>

Elements can have opening tag; and closing tag inside it. Such elements will not hold any text data inside it. Also know as Empty Elements or empty tag.

<firstname />
Elements may contain attributes, text data or other elements inside it know as child elements.Examples:

  • Elements with Text data
    <firstname>Varun</firstname>
  • Element with attribute
    <firstname gender="female">Varun</firstname>
  • Element with Child Elements: All the elements inside parent element are referred as child elements. Here “student” is an parent element to child element “firstname“.
    <student>
        <firstname gender="male">Varun</firstname>
    </student>
Attributes


Attributes are part of elements and are defined only in opening tag of an element. Attributes define addition information to the element, more of a metadata. There can be no or multiple attributes to an element.
Syntax: name=”vaule”

<firstname gender="female">Varun</firstname>

Here we have defined attribute gender=”male”.

 


About Varun Goel

Varun Goel is a technology enthusiast with 6+ years exp in IT industry. In fact, he is been developing application after schooling as freelancer. Currently working with one of the Fortune’s 100 Companies having vast experience Mule ESB, Tibco, HTML5, CSS, JSS, Android, Core Java, JSP, PHP, MySQL, AutoCAD, Maya, ZBrush, Photoshop, Flash CS and many more.

Leave a Reply

Your email address will not be published.