XML

XML Tutorial – From Zero to Confident
XML = eXtensible Markup Language
It’s not a programming language.
It’s a way to store and transport structured data in a very readable format — both for humans and for computers.
Analogy:
Think of XML as a very strict librarian who organizes books using a very clear labeling system.
XML<book>
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<year>1997</year>
<pages>320</pages>
</book>
Everything is labeled clearly — no confusion.

1. Basic Rules of XML (Must Follow All!)

Rule #RuleExample (Correct)Example (Wrong)Why Wrong?1Must have exactly one root element<students>…</students><student>…</student><teacher>…</teacher>More than one root2Tags must be properly nested<p><b>text</b></p><p><b>text</p></b>Crossed tags3Every opening tag must have a closing tag<name>Ram</name><name>RamMissing closing tag4Tag names are case-sensitive<Person> ≠ <person><Person>…</person>Case mismatch5Attribute values must be in quotesid=”101″id=101Missing quotes6No overlapping tags allowed—<b><i>text</b></i>Overlapping
Quick mnemonic: P.N.C.A.Q
Properly Nested, Case-sensitive, All tags Closed, Quotes for attributes

2. Very First XML Example – A Student Record
XML<?xml version=”1.0″ encoding=”UTF-8″?>
<student>
<rollno>101</rollno>
<name>Rahul Sharma</name>
<class>10</class>
<section>A</section>
<marks>
<maths>92</maths>
<science>88</science>
<english>85</english>
</marks>
<isPresent>true</isPresent>
</student>
Line-by-line explanation:

<?xml version=”1.0″ encoding=”UTF-8″?>
→ This is the XML declaration (optional but very good practice)
→ Tells: “This is XML version 1.0” and “characters are encoded in UTF-8″
<student> → Root element (only one allowed)
<rollno>101</rollno> → Element with text content
<marks> → Parent element (contains child elements)
<isPresent>true</isPresent> → Boolean value stored as text

3. Elements vs Attributes – When to Use What?
Real-life example: A book tag
Way 1 – Everything as elements (most readable)
XML<book>
<title>Atomic Habits</title>
<author>James Clear</author>
<year>2018</year>
<language>English</language>
</book>
Way 2 – Some data as attributes (compact)
XML<book year=”2018″ language=”English”>
<title>Atomic Habits</title>
<author>James Clear</author>
</book>
Golden Rules (most people follow this):

Use elements when…Use attributes when…The data can be large or multi-lineThe data is small, fixed, metadataYou want to add child elements laterIt is like a property/labelIt feels like a nounIt feels like an adjectiveExamples: name, address, description, priceExamples: id, code, date, version, status
Very common pattern (recommended):
XML<product id=”P1001″ category=”electronics” discount=”10″>
<name>Wireless Mouse</name>
<price>1499.00</price>
<stock>45</stock>
<description>Ergonomic mouse with 2.4GHz connection</description>
</product>

4. Empty Elements (Two Styles)
Both are correct:
XML<image src=”photo.jpg” />

<!– or –>

<image src=”photo.jpg”></image>
Most people prefer the short form <tag/> when there is no content.

5. Comments in XML
XML<!– This is a comment –>

<!–
Multi-line comment
You can write anything here
It will be completely ignored
–>
Important: Comments cannot appear before the <?xml …?> declaration.
Wrong:
XML<!– wrong place –>
<?xml version=”1.0″?>
Correct:
XML<?xml version=”1.0″?>
<!– correct place –>
<root>…</root>

6. CDATA – When You Want to Store Raw Text (Very Useful!)
Sometimes you want to store code, HTML, JSON, etc. inside XML.
Problem:
XML<description>
<p>This is <b>bold</b> text</p>
</description>
→ XML parser will think <p> and <b> are tags → ERROR
Solution: Use CDATA
XML<description>
<![CDATA[
<p>This is <b>bold</b> text</p>
function sayHello() {
alert(“Hello World!”);
}
]]>
</description>
→ Everything inside <![CDATA[ … ]]> is ignored by the parser — treated as plain text.

7. Real-World Sized Example – Online Order
XML<?xml version=”1.0″ encoding=”UTF-8″?>
<order orderId=”ORD-2025-7841″ date=”2025-06-15″>
<customer>
<id>CUST9876</id>
<name>Akshay Kumar</name>
<email>akshay.k@example.com</email>
<phone>+91-9876543210</phone>
</customer>

<shippingAddress type=”home”>
<street>Flat 403, Green Park Society</street>
<city>Hyderabad</city>
<state>Telangana</state>
<pincode>500081</pincode>
</shippingAddress>

<items>
<item id=”P784″>
<name>Bluetooth Speaker</name>
<quantity>1</quantity>
<unitPrice>3499.00</unitPrice>
<total>3499.00</total>
</item>
<item id=”P129″>
<name>Phone Case</name>
<quantity>2</quantity>
<unitPrice>499.00</unitPrice>
<total>998.00</total>
</item>
</items>

<payment>
<method>UPI</method>
<status>paid</status>
<transactionId>UPI-REF-52639874</transactionId>
</payment>

<totalAmount currency=”INR”>4497.00</totalAmount>
</order>
This is a very typical structure used in e-commerce, APIs, invoices, etc.

8. Common Mistakes Beginners Make

Forgetting the closing tag
Wrong nesting
Using & without escaping → write &amp; instead
Putting < or > in text without escaping → use &lt; and &gt;
Multiple root elements
Case mismatch: <Name> vs </name>
Missing quotes around attribute values
Writing comment before XML declaration

Quick escaping table:

CharacterEscape as<<>>&&”””
Example:
XML<message>
Price is &lt; 100 &amp; discount &gt; 20%
</message>

9. Summary – What You Should Remember

XML is structured, readable, hierarchical data format
One root element only
Properly nested and closed tags
Case-sensitive
Attributes in quotes
Use elements for data, attributes for metadata
Use CDATA when storing code/HTML/JSON
Escape special characters: & → &, < → <, etc.

Would you like to go deeper into any of these topics next?

XML Namespaces (very important for real projects)
XML Schema (XSD) – defining rules
Attributes vs Elements – more examples & decision guide
Parsing XML in JavaScript / Python / Java
Converting JSON ↔ XML
Creating your own small XML project

Just tell me what you want to practice or understand more deeply! 😄