News & UpdatesProgrammingWeb programming Store My Projects
Links
Affiliates

XML Tutorial – 03 – Syntax Rules II

XML documents use a very simple and self-describing syntax. If you open up the document created in the previous section in a browser it will be displayed as a tree without any stylistic formatting. It is just data wrapped in tags. To make any use of the file a program has to be written that accesses the data.

<?xml version="1.0" encoding="UTF-8"?>
<book>
  <title>Learning XML</title>
  <chapter>What is XML</chapter>
</book>

Keep in mind that XML has very strict rules regarding its structure. If there is an error in the document, for example if a closing tag has been nested improperly, the browser will report the error and won’t display the document.

<a></a><b>Allowed</b>
<a><b>Allowed</b></a>
<a><b>Invalid</a></b>

Content types

Elements can have four different content types. Below the title element has simple content, which is also called text content. Book has element content, because it contains only elements and no text. Chapter has mixed content, because it contains both text and elements. Finally, the para tag has empty content. Elements with empty content can also be shortened as in XHTML, by removing the end tag and including a slash before the closing bracket of the start tag.

<?xml version="1.0" encoding="UTF-8"?>
<book>
  <title>Text content</title>
  <chapter>Mixed content
    <para></para>
    <para />
  </chapter>
</book>

Comments

The comment tag can be useful for leaving notes or for temporarily removing a block of XML data. It has the same notation as the one used in HTML. The tag that may span multiple lines and can appear anywhere in the document.

<!-- This is an XML comment -->