In this tutorial you will learn about the XML Document and its application with practical example.
In this lesson we’ll demonstrate you to create a well formed XML Document, it is very easy to create XML Document as there is predefined specification for creating XML Document –
Let’s take a look at the example below that demonstrate the standard structure for XML Document:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE document system "inbox.dtd"> <!-- Here is a comment --> <?xml-stylesheet type="text/css" href="inbox.css"?> <inbox> <message id="1"> <from>John</from> <subject>Test Message 1</subject> <body>Hello John!</body> </message> <message id="2"> <from>Chris</from> <subject>Test Message 2</subject> <body>Hello John!</body> </message> <message id="3"> <from>Adam</from> <subject>Test Message 3</subject> <body>Hello John!</body> </message> </inbox> |
An XML Document mainly consist of two parts that is prolog and body.
XML Prolog
This is first and most important part of an XML Document. It tells the browser that this document is marked up in XML.The prolog contains an XML declaration, possibly followed by a document type declaration (DTD).
XML Declaration
When we look at the XML Document we notice first line as below –
1 |
<?xml version="1.0" encoding="iso-8859-1"?> |
It is used as the XML declaration it tell the browser that the document is written in XML and specifies which version of XML. It also specify the language of character encoding.
Although XML declaration is not necessary but it is best practice to include it in your XML documents.
Document Type Definition (DTD)
Document Type Declaration is used to specify the set of rules for current XML Document structure as per the requirement. Set of rules or instructions can be defined in a separate file and this file can be used to validate XML document in which it is included and application stops to process the XML Document further if the document doesn’t adhere to the DTD.
Example
1 |
<!DOCTYPE document system "inbox.dtd"> |
XML Comments
XML comments begin with <!--
and ends with -->
. Like any other programming language XML Comments is used to add annotations or remarks to enhance the readability of the XML Code for the programmers. Comments can be placed anywhere in an XML document.
Example
1 |
<!-- Here is a comment --> |
XML Body
The body of XML Document is consists of a container element know as root element and other elements placed inside the root element.
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
<inbox> <message id="1"> <from>John</from> <subject>Test Message 1</subject> <body>Hello John!</body> </message> <message id="2"> <from>Chris</from> <subject>Test Message 2</subject> <body>Hello John!</body> </message> </inbox> |