Chapter 1: XML Home
What is an “XML Home” / XML Document?
An XML document is the complete file or message that contains:
- (optional) XML declaration
- (optional) comments, processing instructions
- Exactly one root element (this is the most important rule!)
- All the content inside that root element
Think of the XML document as a house:
- The foundation → XML declaration (tells what version and encoding we use)
- The main door / entrance → the root element (only one allowed!)
- The rooms inside → all other elements, attributes, text, etc.
Real-life structure of a complete XML document
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?xml version="1.0" encoding="UTF-8"?> <!-- This is a complete XML document (sometimes called "XML home") It represents one invoice --> <invoice documentNo="INV-2025-0789" issueDate="2025-06-20" dueDate="2025-07-20"> <!-- Company / Seller information --> <seller> <name>TechTrend Innovations Pvt Ltd</name> <gstin>36AAECT4567P1Z2</gstin> <address> <street>Plot No. 45, Hi-Tech City Road</street> <city>Hyderabad</city> <state>Telangana</state> <pincode>500081</pincode> <country>India</country> </address> <contact>+91 40 1234 5678</contact> <email>billing@techtrend.in</email> </seller> <!-- Buyer / Customer information --> <buyer> <name>Creative Minds Academy</name> <gstin>29AABCM9876Q1Z5</gstin> <address> <street>3rd Floor, Orion Mall</street> <city>Bengaluru</city> <state>Karnataka</state> <pincode>560055</pincode> </address> <contactPerson>Ms. Priya Sharma</contactPerson> <email>accounts@creativeminds.edu</email> </buyer> <!-- List of items sold --> <items> <item lineNo="1"> <code>SW-LIC-ENT-2025</code> <description>Enterprise License - Learning Management System (1 year)</description> <quantity>1</quantity> <unitPrice>185000.00</unitPrice> <discountPercent>10</discountPercent> <taxRate>18</taxRate> <subtotal>166500.00</subtotal> </item> <item lineNo="2"> <code>TRN-001</code> <description>Training & Onboarding Support (16 hours)</description> <quantity>1</quantity> <unitPrice>48000.00</unitPrice> <taxRate>18</taxRate> <subtotal>48000.00</subtotal> </item> </items> <!-- Summary calculations --> <summary> <subTotalBeforeTax>214500.00</subTotalBeforeTax> <totalTax>38610.00</totalTax> <grandTotal currency="INR">253110.00</grandTotal> <amountInWords>Two Lakh Fifty Three Thousand One Hundred Ten Rupees Only</amountInWords> </summary> <!-- Payment instructions --> <paymentInfo> <bankName>HDFC Bank</bankName> <accountNumber>50200xxxxxxxxx78</accountNumber> <ifsc> HDFC0000123</ifsc> <accountHolder>TechTrend Innovations Pvt Ltd</accountHolder> <upiId>techtrend@hdfcbank</upiId> <note>Payment due within 30 days. Late payment will attract 18% interest p.a.</note> </paymentInfo> <!-- Optional footer / legal terms --> <terms> <term>Goods once sold will not be taken back or exchanged.</term> <term>All disputes subject to Hyderabad jurisdiction only.</term> </terms> </invoice> |
This is a complete, valid XML document — this is what people mean when they say “the XML file” or “the XML home”.
Breaking down every part of the “XML Home”
| Part | Mandatory? | Purpose / Meaning | Example from above |
|---|---|---|---|
| XML Declaration | No (but recommended) | Tells parser: version + encoding | <?xml version=”1.0″ encoding=”UTF-8″?> |
| Comments | No | Notes for developers — completely ignored by parsers | <!– Company / Seller information –> |
| Root Element | Yes | The single parent that contains everything else | <invoice …> … </invoice> |
| Attributes on root | No | Usually document number, date, version, status | documentNo=”INV-2025-0789″ |
| Child elements | No | Main sections of data | <seller>, <buyer>, <items>, <summary> |
| Nested elements | No | Deeper structure (like address inside seller) | <address> inside <seller> |
| Text content | No | Actual values | Hyderabad, 253110.00, Two Lakh … |
| Empty elements (self-closing) | No | Tags with no content | <isPaid/> (not shown here, but common) |
| CDATA sections | No | Raw text that contains <, >, & etc. | (not used here, but useful for HTML or code) |
Most common root element names people use in real life
| Industry / Purpose | Very common root names |
|---|---|
| E-commerce, orders | <order>, <Order>, <PurchaseOrder>, <SalesOrder> |
| Invoices / Billing | <invoice>, <Invoice>, <Bill>, <eInvoice> |
| Products / Catalog | <catalog>, <products>, <productList> |
| Configuration files | <configuration>, <settings>, <config> |
| Students / School | <student>, <students>, <classroom> |
| Employees / HR | <employee>, <employees>, <payroll> |
| API responses | <response>, <result>, <data>, <root> |
| Medical / Healthcare | <patient>, <clinicalDocument>, <cda> |
Important rule: There can be only one root element. This is wrong:
|
0 1 2 3 4 5 6 7 8 |
<!-- INVALID XML DOCUMENT --> <student>...</student> <student>...</student> |
This is correct:
|
0 1 2 3 4 5 6 7 8 9 10 |
<!-- VALID – one root --> <students> <student>...</student> <student>...</student> </students> |
Typical variations of XML declaration (what you will see)
| Situation | Declaration you will see |
|---|---|
| Most common today | <?xml version=”1.0″ encoding=”UTF-8″?> |
| Very old systems | <?xml version=”1.0″?> |
| When using special characters (Hindi, etc.) | <?xml version=”1.0″ encoding=”UTF-8″?> (must!) |
| Standalone document (no external DTD) | <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?> |
Recommendation for 2025: Always write:
|
0 1 2 3 4 5 6 |
<?xml version="1.0" encoding="UTF-8"?> |
Quick Checklist – Is this a valid “XML Home”?
- Starts with XML declaration (or not — but better if it does)
- Has exactly one root element
- All tags are properly opened and closed
- All tags are correctly nested (no crossing)
- Attribute values are in quotes (“…” or ‘…’)
- No unescaped <, >, & in normal text (unless inside CDATA)
- Tag names are case-sensitive ( <Invoice> ≠ <invoice> )
Practice – Which of these are valid XML documents?
|
0 1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <book> <title>Learn XML</title> </book> |
→ Valid
|
0 1 2 3 4 5 6 7 8 |
<book> <title>Learn XML</title> <book> |
→ Invalid (missing closing tag)
|
0 1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <order id="123"/> <order id="456"/> |
→ Invalid (two root elements)
|
0 1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="UTF-8"?> <root> <data>Hello & World</data> </root> |
→ Invalid (unescaped &)
|
0 1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="UTF-8"?> <root> <data><![CDATA[Hello & World <test>]]></data> </root> |
→ Valid
Would you like to continue with one of these next?
- How to write very clean & professional XML (naming conventions)
- Difference between well-formed vs valid XML
- What are XML Namespaces and why they exist
- How to use CDATA, comments, and processing instructions
- Creating a small real project (example: student report card XML)
- Common real-world XML formats (SOAP, RSS, SVG, Office Open XML, etc.)
Tell me which direction feels most useful for you right now! 😊
