Theory
The parser said no
Your first hand-written events.xml refuses to load. The browser shows a cold, single-line rejection with a line number, and no page.
HTML never treated you this way. You forgot a closing tag for YEARS and browsers quietly forgave.
XML's grammar is small (you can hold all of it in one lesson) but every rule is enforced. Learning them as rules-with-reasons now saves an hour of line-number archaeology every week of this unit.
Theory
The declaration
An XML file may open with the XML declaration:
<?xml version="1.0" encoding="UTF-8"?>
It announces the XML version and the character encoding (UTF-8 covers Gujarati event names in events.xml). It is optional, but when present it must be the very first thing in the file: not even a blank line or comment above it. A full lesson on its rules closes this unit; today, recognise it and place it first.
Theory
Elements: the building blocks
An element is start tag, content, end tag:
<venue>Main Ground</venue>
Rules with teeth:
- every element closes: XML has no forgiving unclosed <br>; even empty ones close
- empty elements self-close:
<online/>is shorthand for<online></online> - names are case-sensitive, contain no spaces, and must not start with a digit or punctuation (
<2024event>is illegal;<event2024>is fine) - content may be text, other elements, or both
Practical
events.xml, annotated against the rules
<?xml version="1.0" encoding="UTF-8"?> <!-- declaration: first thing -->
<events> <!-- root element opens -->
<event id="1" type="cultural"> <!-- attributes: quoted values -->
<name>Garba Night</name> <!-- start tag + content + end tag -->
<venue>Main Ground</venue>
<online/> <!-- empty element, self-closed -->
</event> <!-- closes before its parent -->
</events> <!-- root closes last -->
Theory
Attributes and the nesting law
Attributes ride on the start tag as name="value" pairs: <event id="1" type="cultural">. The quotes are mandatory: id=1 is a syntax error in XML (HTML shrugged at it).
Nesting must be properly contained: an element closes before its parent does. <b><i>text</b></i> crosses the streams and is rejected.
Recognise the discipline? Tags close in reverse order of opening: last opened, first closed. That is BCA304's stack, LIFO, running the grammar. A document obeying every rule on this page earns the official adjective: well-formed.
Quiz
Which of these lines is well-formed XML?
- <event id=1><name>Garba</name></event>
- <event><name>Garba</event></name>
- <event id="1"><name>Garba</name></event>
- <Event><name>Garba</name></event>
Show the answer
<event id="1"><name>Garba</name></event>
Option C quotes its attribute, closes both elements, and nests them without crossing: every rule satisfied. Option A loses on the unquoted id=1, the habit HTML tolerated and XML never will. Option B crosses the nesting: name opened inside event must close BEFORE event does (last opened, first closed). Option D trips case-sensitivity: <Event> and </event> are different names, so the root never closes. Exams build this exact question from these exact 3 mistakes; check quotes, crossing, case, in that order.
Think first
Element or attribute?
The event's id sits in an attribute (id="1") while its venue sits in a child element. Before tapping: is there a rule forcing that split, and what is the working convention?
Show the answer
No hard rule: XML would accept venue as an attribute or id as an element. The working convention: attributes for small identifying metadata (ids, types, flags), elements for the data itself (names, venues, times), because elements can nest, repeat and grow (2 venues? add another element; attributes cannot repeat on one tag). Exams accept either design if you can defend it; real feeds like RSS follow exactly this metadata-vs-data split.
Watch out
The 4 rejection slips
When events.xml refuses to load, it is one of these, in likelihood order:
- an unclosed element somewhere (the line number points near, not at, the crime)
- unquoted attribute: id=1
- case mismatch: <Name> closed by </name>
- crossed nesting: closing the parent before the child
And from BCA305-02: Android Studio flagged these same 4 in your layouts. Same grammar, same police.
Theory
Well-formed is a checklist, use it
Before blaming the parser, run the 5-point inspection: declaration first (if present)? every tag closed? attributes quoted? cases matching? nesting uncrossed? That checklist IS the definition of well-formed, and writing it as a list answers the standard 5-mark "state the rules of XML syntax" question verbatim. Two rules got only a nod today: the single ROOT element and case-sensitivity's fine print. They headline the next lesson.
Summary
Key takeaways
- The declaration <?xml version="1.0" encoding="UTF-8"?> is optional, but if present it comes absolutely first.
- Element = start tag + content + matching end tag; everything closes, empties self-close (<online/>).
- Attributes are quoted name="value" pairs on the start tag; unquoted values are errors.
- Names: case-sensitive, no spaces, never starting with a digit.
- Nesting never crosses: last opened, first closed (BCA304's LIFO in grammar form).
- Obey all of it and the document is WELL-FORMED; parsers reject anything less.
- Memory hook: quote it, close it, uncross it, match its case.