Main menu

Learn XML in 15 min

eXtensible Markup Language is a tag based markup language for storing data. It’s a self-describing data which store its structure and data both. It’s crated for sharing data between heterogeneous systems and platforms.

Let’s take an example: we can export database in XML format can import it on other databases, spreadsheets and application.

If we have a web website with very frequent updates, changing the HTML content all the time is not a good idea. We can parse the data from XML and can render the HTML content in real time.

An application produce data in XML format so other application can use that data.


 XML Syntax

XML is a markup language like HTML. Where HTML is designed for display data XML is designed for storing and carrying data.

Things to remember: –

  •   There isn’t any predefine tags like HTML. All the tags are defined by author.
  •   XML Tags are Case Sensitive
  • XML documents that conform to the syntax rules is called Well Formed XML

Let’s have a look at XML document in tree structure.

 <root>
   <child>
       <subchild> data </subchild>
   <child>
   <child>
       <subchild> data </subchild>
   <child>
 </root>

Let say we have some information which we want in XML format.
Name: zoladius
Email: something@gmail.com
Address: Indore (India)

<?xml version="1.0" encoding="utf-8"?>
<Users>
<Name> zoladius <Name>
<Email>something@gmail.com<Email>
<Address> Indore (India)<Address>
</Users>

Save this with extension .xml and use web browser to display it’s content.
First line define the xml version and default encoding type.

Note: Every xml tag must have a opening and closing tags like <Users> </Users>
Note: Every xml document must have a root tag in our document it’s <Users> </Users>. Note: Note: White-space is preserved in XML
Note: XML documents must contain one element that is the parent of all other elements. This element is called the root element.

Comments in XML document: Comments serve a note for developer. It’s not used as a part of XML data.
We can use html like comments in XML documents.

<!--This is a comment -->

XML attribute: Attributes is used to identifying and describing elements.
Note: XML Attribute Values Must be quoted.

<Sector19>
 <User name=”admin”>
 <Email>something@gmail.com<Email>
 <Address> Indore (India)<Address>
 <User>
 <User name=”zolan”>
 <Email>zolan@sector19.net<Email>
 <Address> Lompoc (Los Angeles)<Address>
 <User>
 </Sector19>

XML Reserved characters

There are some reserved characters on xml. If we use these characters on data it will be interpreted wrong and will produce and error.
If we parse this XML document it will generate an error that XML Parsing Error: not well-formed
Because & is an syntax in XML document and we cannot use it in data.

<Users>
 <Name> zoladius & zolan <Name>
 <Email>something@gmail.com<Email>
 <Address> Indore (India)<Address>
 </Users>

To avoid this error reserved characters must be replaced with entity reference.

&lt; < less than
&gt; > greater than
&amp; & ampersand
&apos; apostrophe
&quot; quotation mark
 <Users>
 <Name> zoladius &amp; zolan </Name>
 <Email>something@gmail.com</Email>
 <Address> Indore (India)</Address>
 </Users>

URI: A Uniform Resource Identifier (URI) is a value which used to identifying the internet resources. Unified Resource Locater (URL) is an example or URI which is used to locate the address of web resource of domain.


Namespace

Namespace is used to avoid conflicts between tags. Tags are often defined by a developer so they often conflict with other XML application. We can reduce the risk by using name prefix.

Example: I have a video blog, people search on my website for videos. I have XML data of various video websites. If person search for photography by “Donald Zolan” and two providers using the same tag name for different purpose it will make conflict in search.
Another example like we have a XML document with HTML tags to make it more readable. We have a title tag for our books. If we query for title we want the books of title not the title of our HTML document so it can make a conflict.

<Users xmlns:z=”http://technet.sector19.net/myXML">
 <Name> zoladius </Name>
 <Email>something@gmail.com</Email>
 <Address> Indore (India)</Address>
 </Users>
xmlns:prefix=URI

xmlns is a XML predefine attribute. Prefix is just an string for storing the URI so you don’t have to write long URI in your XML document.
Note: if we not using prefix in child tags it use the default prefix defined in parent.

<Users xmlns:h="http://www.w3.org/HTML/1998/html4" xmlns:z=”http://technet.sector19.net/myXML">
<h:table>
<h:tr><z:Name> zoladius </z:Name></h:tr>
<h:tr><z:Email>something@gmail.com</z:Email></h:tr>
<h:tr><z:Address> Indore (India)</z:Address></h:tr>
<h:table>
</Users>

In above example we are using two namespace one for HTML tags and one for our XML document tags. So we easily can process the XML data using namespace and it won’t be conflict.


  XML based or related technology

  1. XHTML
  2. XSLT is an XML language that can be used to transform XML documents into other formats, like HTML.
  3. WSDL for describing available web services
  4. WAP and WML as markup languages for handheld devices
  5. RSS languages for news feeds
  6. RDF and OWL for describing resources and ontology
  7. SMIL for describing multimedia for the web

XML Tools

  1. XML tools
  2. Firebug Xpath
  3. Xpath Checker
  4. Xpath Runner
  5. Firexpath
  6. sketchpath
Table Table
Table Table
FacebookTwitterGoogle+RSS