BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl
Home / XML Tutorial / XML Syntax

XML Syntax

By Chaitanya Singh

In this tutorial, we will learn the syntax of XML with the help of examples.

1. Root Element is mandatory in XML

XML document must have a root element. A root element can have child elements and sub-child elements.

For example: In the following XML document, <message> is the root element and <to>, <from>, <subject> and <text> are child elements.

<?xml version="1.0" encoding="UTF-8"?>
<message>
   <to>Steve</to>
   <from>Paul</from>
   <subject>Message from teacher to Student</subject>
   <text>You have an exam tomorrow at 9:00 AM</text>
</message>

The following XML document is wrong because it doesn’t have a root element.

<?xml version="1.0" encoding="UTF-8"?>
<to>Steve</to>
<from>Paul</from>
<subject>Message from teacher to Student</subject>
<text>You have an exam tomorrow at 9:00 AM</text>

2. XML is case sensitive

XML is a case sensitive language.

For example:
This is valid

<from>Paul</from>

This is invalid
The first letter of closing tag is in capital while the first letter of opening tag is in small, this is an example of invalid XML.

<from>Paul</From>

3. XML Prolog

<?xml version="1.0" encoding="UTF-8"?>

This line is called the XML Prolog. It is an optional line, however it should be the first line when you mention it. It specifies the XML version and the encoding used in the XML document.

4. Elements should not overlap

All the elements in XML should be properly nested and they should not overlap.

<class><teacher>Rick</class></teacher>  -->Wrong (Not nested properly)
<class><teacher>Rick</teacher></class>  -->Correct (Correctly nested)

5. Attributes in XML

We will discuss XML attributes in detail later. For now, lets see the syntax of attributes. An opening tag in XML can have attributes, these attributes are name & value pairs.

Attribute names are case sensitive and should not be in quotation marks.
Attribute values should be in single or double quotation.

<text category = "message">You have an exam tomorrow at 9:00 AM</text>

Here category is the attribute name and message is the attribute value.

Lets take few more examples to see valid and invalid cases of attributes.
A tag can have more than one name & value pairs, however two attribute names cannot be same (see point 5 in the following example)

1. <text category = message>hello</text>  -->wrong
2. <text "category" = message>hello</text>  -->wrong
3. <text category = "message">hello</text>  -->correct
4. <text category = "message" purpose = "greet">hello</text>  -->correct
5. <text category = "message" category ="greet">hello</text>  -->wrong

6. XML elements must have a closing tag

All XML documents must have a closing tag.

<text category = message>hello</text>  -->correct
<text category = message>hello  -->wrong

7. Comments in XML

This is how a comment should look like in XML document.

<!-- This is just a comment -->

8. White-spaces are preserved in XML

Unlike HTML that doesn’t preserve white space, the XML document preserves white spaces.

Posted Under: XML Tutorial

Comments

  1. Dmanuele says

    March 30, 2021 at 6:05 PM

    Please compare section 5 and 6. When I pasted what I thought was an issue, it was interpreted by HTML and present the syntax.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap