Wednesday 29 August 2012

Simple XML basics


Basically XML is designed to transport and store the data.XML is not replacement to HTML, we can say it is complementary to HTML. HTML is used to display data and XML is used to carry data.
        You must define your own tags, usually these tags are self descriptive. Though XML give us the freedom to create user defined tags with any name and any hierarchy but still we can define the format for XML document using DTD (Document Type Definition) or Schema. DTD and Schema defines the rules for XML document. In object oriented perspective we can say, DTD and Schema is like Class and XML file is object of the class. DTD defines the rules for XML document. We can validate the XML file against DTD or Schema.  We can also validate the XML grammar online. Schema is W3 recommendation and currently schema is very emerging. In few years DTD will be out of picture. Schema is very much easier to learn as Schema itself is an XML document. But is not true in case of DTD, as DTD itself is an different language. We can provide the schema inside XML or even outside the XML. w can provide the external Schema using  schemaLocation parameter in root tag of the XML.
        While defining the user defined tags developers always face the problem of tag name conflict. XML specs provide solution for this problem ie. XML namespace(xmlns). We can define a tags associated with xmlns.
<root>
<html:table xmlns:html="http://www.w3.org/TR/html4/">
  <html:tr>
    <html:td>Apples</html:td>
    <html:td>Bananas</html:td>
  </html:tr>
</html:table>

<furniture:table xmlns:furniture="http://www.w3schools.com/furniture">
  <furniture:name>Coffee Table</furniture:name>
  <furniture:width>70</furniture:width>
  <furniture:length>140</furniture:length>
</furniture:table>

</root>

Namespace value (eg xmlns:furniture="") is for reader's clarity only, its value do not signify anything.