XML Documents structure



In this tutorial we are going to learn about XML Document. XML Document consists of many parts. XML Document have 2 main parts i.e Document Information Followed by Document Body.

Consider Following Document -

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE document system "Person.dtd">
<!-- Here is a comment -->
<?xml-stylesheet type="text/css" href="Styles.css"?>
<Person>
  <Male>
    <name>Pritesh</name>
    <age>22</age>
  </Male>
  <Female>
    <name>Pooja</name>
    <age>22</age>
  </Female>
</Person>

Explanation : XML Document

Part 1 : Prolog (optional)

1.1 XML Declaration :

  1. XML Declaration is Optional.
  2. XML Declaration must be First Line in XML Document if we write Declaration.
  3. XML Declaration tells that Document Written is in XML.
  4. XML Declaration tells XML Version used to Write Document.
  5. XML Declaration tells Encoding Style Used to Encode XML Document.
  6. If XML Document is standalone i.e if it does not depends on other external document then we need to specify standalone=”yes”.
  7. W3C recommends to include XML Declaration.
<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>

1.2 Document Type Definition (DTD)

  1. Document Type definition is used to Define XML Document.
  2. DTD is used when you Validate your XML document.
  3. DTD can be Internal or External.
  4. DTD rule tells which Element is allowed to nest inside Other Element.
<!DOCTYPE document system "Person.dtd">

1.3 Comment

  1. Comments are Optional part of XML Document.
  2. Comments in XML are similar to HTML . <!- and ->
  3. Content Written inside Comment is ignored by Parser. (Comment part is not parsed by Parser)
  4. Comments can appear anywhere inside XML Document.

1.4 Styling and Processing Instruction

  1. Processing Instructions begin with <? and ends with ?>
  2. Processing Instructions are instructions for the XML processor.
  3. Processing instructions are processor dependant so not all processors understand all processing instructions.
<?xml-stylesheettype="text/css"href="Styles.css"?>

1.5 White Space

  1. White Space can be created using Carriage Return , Line Feed and Tab.
  2. White Space cannot affect Parsing of Document.
  3. User is Free to Use White Space anywhere inside document.
  4. XML recommendation specifies that XML documents use the UNIX convention for line endings.
  5. It means that you should use a linefeed character only (ASCII code 10) to indicate the end of a line.

Summary :

XML Declaration
<?xml version="1.0" encoding="UTF-8"
standalone="no"?>
Document Type Definition (DTD)
<!doctype document system "Person.dtd">
Comment
<!-- Here is a comment -->
Processing Instructions
<?xml-stylesheet type="text/css"
href="Styles.css"?>
White Space