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 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.
Address: Indore (India)
<?xml version="1.0" encoding="utf-8"?> <Users> <Name> zoladius <Name> <Email>email@example.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>firstname.lastname@example.org<Email> <Address> Indore (India)<Address> <User> <User name=”zolan”> <Email>email@example.com<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>firstname.lastname@example.org<Email> <Address> Indore (India)<Address> </Users>
To avoid this error reserved characters must be replaced with entity reference.
<Users> <Name> zoladius & zolan </Name> <Email>email@example.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 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>firstname.lastname@example.org</Email> <Address> Indore (India)</Address> </Users>
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>email@example.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
- XSLT is an XML language that can be used to transform XML documents into other formats, like HTML.
- WSDL for describing available web services
- WAP and WML as markup languages for handheld devices
- RSS languages for news feeds
- RDF and OWL for describing resources and ontology
- SMIL for describing multimedia for the web
- XML tools
- Firebug Xpath
- Xpath Checker
- Xpath Runner