XML stands for Extensible Markup Language. Extensible means user can defined his own tag. XML was designed by W3C (World Wide Web Consortium). XML is not a programming language it is a text based markup language to store & define data. XML was derived from Standard Generalized Markup Language (SGML). It is open standard & free to use commercially.
Use & Advantages of XML
- For large websites XML work behind the scene to simplify the creation of HTML documents.
- XML is flexible to store & organize data.
- Style-sheet can easily merge with XML to create any kind of look n feel.
- XML can be used for reloading & off-loading data from databases.
- Any kind of data type can be presented using XML.
- XML is a lightweight & platform independent technology.
Introduction to XML Syntax
To start with a XML document the first statement is declaration. In declaration you need to define the version & encoding type for XML. Keep remember XML is case-sensitive. <?xml> is not same as <?XML>.
<?xml version="1.0" encoding="UTF-8"?>
Then in body you have tags & elements. Look at the example below.
<?xml version="1.0" encoding="UTF-8"?> <employee> <name>Biswabhusan Panda</name> <organization>Vijayshanti Infotech (p) Ltd.</organization> <mobile>9096266548</mobile> </employee>
Here you can notice there are two kind of information. One is tag & other one is data. I mean under employee tag we have 3 elements name, organization & mobile. Each element contains some data. While reading XML this root node relationship helps in data binding.
Syntax Rule for XML
1. Element Syntax – While creating a element like tag never forget to close the tag. Always check start & end tag must be identical. For an example for element <name> there must be </name>. XML element name can contain any alphanumeric characters.
2. Nested Elements – XML supports nested elements. While creating maintain correct hierarchy.
3. Root Element – An XML document can have only one root element. Inside the root element you can have nested elements. Root Element is similar like database name. This is the unique entry through which you can retrieve data from your XML document.
4. Case-Sensitive – XML is case-sensitive. <name> is not equal to <Name>.
5. Rule for Attributes – Attribute specify single property of an element. Same attribute can’t have more then one value. Attributes comes with key value pair. While assigning a value to attribute use quotation mark. For an example encoding=”UTF-8″.
6. XML Text – To avoid character encoding problems don’t forget to declare encoding type in your XML file. Whitespace characters (line-breaks, blanks & tabs) must be ignored. Few characters are reserved by the XML syntax itself. So they cannot be used directly. For example < in place of this you can use & lt;.
7. Comments in XML – To add comment in your XML file you can use
<-- Your Comments -->.
To prepare a valid XML document its mandatory to follow XML tree structure. Any XML document must contains a parent element which is called root. Followed by root element child & sub-child elements can be added. For an example if I want to display a persona information you can do like the below.
<person> <name>Biswabhusan</name> <designation>Sr. Process Lead</designation> <address> <city>Pune</city> <state>Maharashtra</state> <country>India</country> </address> </person>
Here person is the root element. Name, Designation & Address are the child elements. Under Address City, State & Country are the sub-child elements.
XML DTD (Document Type Definition)
XML is a data exchange platform. During data exchange DTD is the technique which helps to validate data for a well formatted XML. DTD can be declare in 2 ways Internally with a XML Document or Externally using a *.dtd file. Using DTD you can verify input data for your XML file. Look at the example below.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE employee [ <!ELEMENT employee (name,age,salary,description)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT salary (#PCDATA)> <!ELEMENT description (#PCDATA)> ]> <employee> <name>Biswabhusan</name> <age>32</age> <salary>45,000</salary> <description>This is only for Sr. Team Leads.</description> </employee>
#PCDATA means parse-able text data.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE note SYSTEM "external.dtd"> <employee> <name>Biswabhusan</name> <age>32</age> <salary>45,000</salary> <description>This is only for Sr. Team Leads.</description> </employee>
<!ELEMENT employee (name,age,salary,description)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT salary (#PCDATA)> <!ELEMENT description (#PCDATA)>