XML File Structure and Elements

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.

Complete XML example Top ↑

<?xml version="1.0" encoding="UTF-8"?>
<addressBook>
  <!-- One contact entry -->
  <contact id="C1">
    <name>Asha</name>
    <city>Hyderabad</city>
  </contact>
</addressBook>

XML declaration Top ↑

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.”

Document element or root element Top ↑

Every XML document has exactly one top-level document element. Other elements are nested inside it.

XML elements Top ↑

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.

Parent, child and sibling elements Top ↑

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.

XML comments Top ↑

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

Comments cannot contain the sequence -- inside the comment text.

Well-formed XML checklist Top ↑

  • One document element.
  • Matching, case-sensitive tags.
  • Proper nesting.
  • Quoted attribute values.
  • No duplicate attributes on the same element.
  • Reserved characters escaped where required.

Next XML topic Top ↑

What is XML? XML attributes

Frequently Asked Questions Top ↑

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.





plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer