A well-formed XML document is a tree with exactly one document element at the top. Inside it you can place nested elements, text, comments and attributes according to the rules of the XML format you are using.
<?xml version="1.0" encoding="UTF-8"?>
<addressBook>
<!-- One contact entry -->
<contact id="C1">
<name>Asha</name>
<city>Hyderabad</city>
</contact>
</addressBook>
The XML declaration is optional. When present it appears at the start of the document. The most common form identifies XML 1.0 and an encoding such as UTF-8.
<?xml version="1.0" encoding="UTF-8"?>
The optional standalone pseudo-attribute can indicate whether the document depends on external markup declarations. It is not simply a switch for “internal DTD” versus “external DTD.”
Every XML document has exactly one top-level document element. Other elements are nested inside it.
Elements use matching start and end tags, or an empty-element form when there is no content.
<city>Hyderabad</city>
<separator />
XML names are case-sensitive, cannot contain spaces, and should not begin with a digit. Names beginning with the letters xml in any capitalization are reserved by the XML specification for standardization use.
When one element is directly inside another, the outer element is its parent and the inner element is its child. Elements that share the same parent are siblings. Tags must be properly nested.
<!-- This is an XML comment -->
Comments cannot contain the sequence -- inside the comment text.
Does every XML file need an XML declaration?
No. It is optional in many XML documents.
Can an XML file have two root elements?
No. A well-formed XML document has one document element containing the rest of the document tree.
Do XML tags have to close?
Yes. Non-empty elements have matching end tags; empty elements can use the self-closing form.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.