XML declaration and rules of declaration

The XML declaration is the line <?xml version="1.0" encoding="UTF-8"?> that opens the file, and its rules are strict: it must be the very first thing (no space or comment before it), lowercase xml, version stated first, then encoding, then optional standalone, all values quoted.

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The line you have copied without reading

Every XML file in FestConnect opens with <?xml version="1.0" encoding="UTF-8"?>. You have typed it many times. Now read it: it announces that this is XML, which version, and what character set encodes the text.

It looks trivial, but it has some of the strictest rules in XML: it must be first, it must be lowercase, and its attributes have a required order. Examiners love it precisely because it is so rule-bound.

Theory

The label on a shipping crate

A shipping crate has a label stuck on the outside, front and centre: 'Contents: glass. This side up. Version 1.'. It is not part of the goods; it tells the handler how to treat the crate. The XML declaration is that label: not data, but instructions about the file (which version, which encoding). And like a label, it belongs right at the front, read before anything is unpacked.

Theory

The declaration and its parts

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

  • version (required, usually 1.0): must come first.
  • encoding (optional): the character set, commonly UTF-8.
  • standalone (optional, yes/no): whether the document relies on external declarations.

It is a processing instruction (<? ... ?>), not an element, so it has no closing tag. All values are quoted. The declaration itself is optional, but recommended.

Practical

Correct and incorrect declarations

<!-- CORRECT -->
<?xml version="1.0" encoding="UTF-8"?>
<registration> ... </registration>

<!-- WRONG: not the first thing (comment before it) -->
<!-- a fest file -->
<?xml version="1.0"?>

<!-- WRONG: uppercase XML -->
<?XML version="1.0"?>

<!-- WRONG: encoding before version -->
<?xml encoding="UTF-8" version="1.0"?>

Think first

Fix the declaration

A file begins with a blank line, then <?Xml encoding="UTF-8" version="1.0">. List every rule it breaks, then write the corrected line.

Show the answer

Broken rules: (1) there is a blank line before it, the declaration must be the very first thing; (2) `Xml` is wrong case, it must be lowercase `xml`; (3) encoding is before version, but version must come first; (4) it ends with > instead of `?>`. Corrected: <?xml version="1.0" encoding="UTF-8"?> on line one with nothing before it. Four small rules, four easy marks.

Quiz

Which XML declaration is written correctly?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml encoding="UTF-8" version="1.0"?>
  3. <?XML version="1.0"?>
  4. <?xml version=1.0 encoding=UTF-8?>
Show the answer

<?xml version="1.0" encoding="UTF-8"?>

Option A is correct: lowercase xml, version first, encoding second, all values quoted, closed with ?>. B puts encoding before the required-first version. C uses uppercase XML. D omits the quotes around the values, which XML requires. The declaration's fussy rules, first, lowercase, version-first, quoted, are exactly what this question tests.

Watch out

Declaration traps

1. Anything before it: not even whitespace or a comment may precede <?xml ...?>. It is line one.

2. Wrong case or order: it is lowercase xml, and version must come before encoding.

3. Unquoted values or `>` instead of `?>`: values are quoted, and it closes with ?> because it is a processing instruction, not an element.

Theory

XML mastered: back to building

You can now read and write any XML in your project: layouts, the manifest, string resources, and data records. Unit 4 puts that to work: you return to FestConnect's screen and add real Android widgets, hiding the title bar, locking orientation, placing text fields and a Button with an onClick, and popping a Toast, each defined in the XML you now understand and wired up in Java.

Summary

Key takeaways

  • The XML declaration is <?xml version="1.0" encoding="UTF-8"?>: it states the version and character set.
  • version is required and comes first; encoding and standalone are optional and follow, in that order.
  • If present, the declaration must be the very first line with nothing (not even whitespace/comments) before it.
  • The keyword is lowercase xml; all values are quoted; it closes with ?> (a processing instruction, not an element).
  • The declaration is optional but recommended.
  • Memory hook: the label on a shipping crate, front and centre, read first.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from XML (Extensible Markup Language)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

XML declaration and rules of declaration · Mobile Application Development - 1 (option B) · Gri-Learn