Theory
One wrong tag, no build
Android will not build a FestConnect layout if a single tag is malformed: a missing close, an unquoted value, a crossed nesting. XML is strict on purpose, and the rules are few.
This lesson names the parts, the declaration, tags, and elements, and the handful of rules that make XML well-formed. Get these and you can read and fix any XML file in your project or an exam.
Theory
Matching brackets in maths
In maths, every opening bracket needs a closing one, and they must not cross: (a + [b]) is fine, (a + [b)] is wrong. XML tags are the same. <a><b></b></a> nests cleanly; <a><b></a></b> crosses and is invalid. If you can balance brackets, you can nest XML tags, it is the same discipline of open, then close in the right order.
Theory
Declaration, tags, elements
- Declaration: the first line,
<?xml version="1.0" encoding="UTF-8"?>(details next lesson). - Tag: the markup itself, a start tag
<year>and an end tag</year>. An empty element can self-close:<line/>. - Element: a start tag, its content, and its end tag together:
<year>2</year>. Elements can nest. - Attribute: a
name="value"pair inside a start tag; the value is always quoted.
Well-formed means: every tag closes, nesting does not cross, and attributes are quoted.
Practical
Elements, attributes, self-closing
<?xml version="1.0" encoding="UTF-8"?>
<registration id="F-108"> <!-- id is an ATTRIBUTE (quoted) -->
<name>Aditi Shah</name> <!-- element: start + content + end -->
<year>2</year>
<events>
<event>Robotics</event> <!-- nested elements -->
<event>Dance</event>
</events>
<confirmed/> <!-- empty, self-closing element -->
</registration>Think first
Spot what makes it well-formed
In the snippet, point out one element, one attribute, and one self-closing (empty) element. Then say why <name>Aditi Shah on its own would break the file.
Show the answer
An element: <year>2</year> (start tag, content 2, end tag). An attribute: id="F-108" inside the <registration> start tag, value quoted. A self-closing element: <confirmed/>. <name>Aditi Shah alone breaks the file because it has no closing </name>; XML requires every element to be closed, so an unclosed tag makes the document not well-formed and Android refuses to build it.
Quiz
Which line is NOT well-formed XML?
- <event name=Robotics> (attribute value is not quoted)
- <event name="Robotics"></event>
- <confirmed/>
- <year>2</year>
Show the answer
<event name=Robotics> (attribute value is not quoted)
XML requires attribute values to be in quotes, so name=Robotics without quotes is not well-formed (A). The quoted version (B) is correct. A self-closing empty element <confirmed/> is valid (C), and a normal element with content <year>2</year> is valid (D). Unquoted attributes, unclosed tags, and crossed nesting are the three ways XML most often fails to be well-formed.
Watch out
Syntax traps
1. Unquoted attribute values: width=match_parent fails; XML needs width="match_parent".
2. Unclosed tags: every element needs an end tag, or must self-close (<confirmed/>). HTML forgives <br>; XML does not.
3. Crossed nesting: <a><b></a></b> is invalid; close the inner element before the outer, like matched brackets.
Theory
Two rules deserve their own lessons
Two well-formedness rules are big enough to study alone. The next lesson covers the single root element (an XML document must have exactly one outermost element) and case sensitivity (<Name> and <name> are different tags, and their open/close must match). Both are guaranteed exam points and the cause of many 'why won't it build' moments.
Summary
Key takeaways
- XML may start with a declaration: <?xml version="1.0" encoding="UTF-8"?>.
- A tag is the markup (start <year> and end </year>); an empty element self-closes: <confirmed/>.
- An element is start tag + content + end tag, e.g. <year>2</year>; elements can nest.
- Attributes are name="value" pairs in the start tag, and values must be quoted.
- Well-formed means every tag closes, nesting does not cross, and attributes are quoted.
- Memory hook: XML tags balance like brackets in maths.