1. Introduction to XML Documents
An XML (Extensible Markup Language) document is a structured text file that stores and organizes data in a hierarchical format. XML is widely used for data exchange, configuration files, and web services.
Creating an XML document involves defining elements, attributes, and a structure that represents the data logically.
2. Structure of an XML Document
An XML document typically consists of:
- XML Declaration (Optional but Recommended)
- Root Element (Mandatory – Only One)
- Child Elements (Nested inside the Root Element)
- Attributes (Optional – Provide Metadata)
- Comments (Optional – Improve Readability)
- CDATA Sections (Optional – Store Unparsed Data)
Example of a Simple XML Document
<?xml version="1.0" encoding="UTF-8"?>
<company>
<employee id="E101" department="HR">
<name>John Doe</name>
<position>Manager</position>
<salary currency="USD">75000</salary>
</employee>
</company>
<?xml version="1.0" encoding="UTF-8"?>
→ XML declaration<company>
→ Root element<employee>
→ Child element with attributes (id
anddepartment
)<name>
,<position>
,<salary>
→ Nested child elements
3. Steps to Create an XML Document
Step 1: Start with an XML Declaration
The XML declaration defines the version and encoding.
<?xml version="1.0" encoding="UTF-8"?>
version="1.0"
→ Specifies XML versionencoding="UTF-8"
→ Defines character encoding (default is UTF-8)
Step 2: Define the Root Element
The root element is the top-level tag that encloses all other elements.
<store>
<!-- All other elements will go inside -->
</store>
Step 3: Add Child Elements
Define nested elements inside the root element to organize data.
<store>
<product>
<name>Smartphone</name>
<brand>Samsung</brand>
<price>50000</price>
</product>
</store>
Step 4: Use Attributes for Metadata
Attributes provide additional information about elements.
<store>
<product id="P101" category="Electronics">
<name>Smartphone</name>
<brand>Samsung</brand>
<price currency="INR">50000</price>
</product>
</store>
id="P101"
andcategory="Electronics"
→ Attributes in<product>
currency="INR"
→ Attribute in<price>
Step 5: Add Comments (Optional)
Use comments to explain different sections of the XML document.
<!-- This XML stores product details -->
<store>
<product id="P101" category="Electronics">
<name>Smartphone</name>
<brand>Samsung</brand>
<price currency="INR">50000</price>
</product>
</store>
Step 6: Use CDATA for Special Characters (Optional)
CDATA (Character Data) is used to include special characters that might otherwise be interpreted as XML tags.
<description><![CDATA[ This product comes with <b>high performance</b> features. ]]></description>
4. Types of XML Documents
XML documents can be classified into two types:
4.1 Well-formed XML
A well-formed XML follows the correct syntax:
✅ Example of Well-formed XML
<student>
<name>Rahul Sharma</name>
<age>22</age>
</student>
❌ Incorrect Example (Missing Closing Tag)
<student>
<name>Rahul Sharma</name>
<age>22
</student>
A missing closing tag for <age>
makes the XML invalid.
4.2 Valid XML
A valid XML follows both syntax rules and a schema definition (DTD or XSD).
5. Saving and Viewing an XML Document
5.1 Saving an XML File
- Open any text editor (Notepad++, VS Code, Oxygen XML, etc.).
- Write the XML code.
- Save the file with a
.xml
extension, e.g.,products.xml
.
5.2 Viewing an XML File
- Open in a web browser (Chrome, Firefox, Edge).
- Use an XML editor for syntax highlighting and validation.
- Use programming languages (Python, Java, PHP) to parse and manipulate XML.
6. Example of a Complete XML Document
<?xml version="1.0" encoding="UTF-8"?>
<university>
<student id="S101">
<name>Amit Kumar</name>
<course>MCA</course>
<age>24</age>
</student>
<student id="S102">
<name>Priya Sharma</name>
<course>MCA</course>
<age>23</age>
</student>
</university>
7. Best Practices for Creating XML Documents
✅ Use Meaningful Element Names
<employee>
<name>John Doe</name>
</employee>
❌ Avoid Generic Names
<data>
<item>John Doe</item>
</data>
✅ Use Attributes for Metadata, Not Data
<employee id="E101">
<name>John Doe</name>
</employee>
❌ Avoid Storing Main Data in Attributes
<employee name="John Doe" id="E101"/>
✅ Follow Proper Nesting
<book>
<title>XML Guide</title>
</book>
❌ Incorrect Nesting
<book>
<title>XML Guide</book>
</title>
8. Conclusion
- Creating an XML document involves defining a hierarchical structure with elements and attributes.
- A well-formed XML follows proper syntax, while a valid XML conforms to a schema.
- XML files can be viewed, edited, and processed using various tools and programming languages.