By Barry Burd

Every Android app consists of some Java code, some XML documents, and some other information. (The acronym XML stands for eXtensible Markup Language.) You might already be familiar with HTML documents — the bread and butter of the World Wide Web.

Like an HTML document, every XML document consists of tags (angle-bracketed descriptions of various pieces of information). But unlike an HTML document, an XML document doesn’t necessarily describe a displayable page.

Here are some facts about XML code:

  • A tag consists of text surrounded by angle brackets.

  • An XML document may have three different kinds of tags: start tags, empty element tags, and end tags.

    A start tag begins with an open angle bracket and a name. A start tag’s last character is a closing angle bracket.

    An empty element tag begins with an open angle bracket followed by a name. An empty element tag’s last two characters are a forward slash followed by a closing angle bracket.

    An end tag begins with an open angle bracket followed by a forward slash and a name. An end tag’s last character is a closing angle bracket.

  • An XML element either has both a start tag and an end tag, or it has an empty element tag.

  • Elements are either nested inside one another or have no overlap.

    For example, in the following code, a TableLayout element contains two TableRow elements:

    <TableLayout xmlns:android=
          "http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name" />
      </TableRow>
      <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/address" />
      </TableRow>
    </TableLayout>

    The preceding code works because the first TableRow ends before the second TableRow begins. But the following XML code is illegal:

    <!-- The following code isn’t legal XML code. →
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name" />
    <TableRow>
    </TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/address" />
    </TableRow>

    With two start tags followed by two end tags, this new XML code doesn’t pass muster.

  • Each XML document contains a root element — one element in which all other elements are nested.

  • Different XML documents use different element names.

    In every HTML document, the <br /> element stands for line break. But in XML, the names RelativeLayout and TextView are particular to Android layout documents. And the names portfolio and trade are particular to financial product XML (FpML) documents. The names prompt and phoneme are peculiar to voice XML (VoiceXML). Each kind of document has its own list of element names.

  • The text in an XML document is case-sensitive.

    For example, if you change RelativeLayout to relativelayout, the app won’t run.

  • Start tags and empty element tags may contain attributes.

    An attribute is a name-value pair. Each attribute has the form name=value. The quotation marks around the value are required.

  • A non-empty XML element may contain content.

    For example, in the element <string name=”hello_world”>Hello world!</string>, the content Hello world! is sandwiched between the start tag (<string name=”hello_world”>) and the end tag (</string>).