What is an XML file?

An XML file is a file which has .xml file extension and the file contains data which is defined using XML markup language.

XML markup language is created by the World Wide Web Consortium (W3C) to define a syntax for encoding documents that both humans and machines could read.

W3C is an international community made up of representatives from many big companies that develops open standards to ensure the long-term growth of the Web.

XML markup page in simple terms

An XML markup page contains:

  • Data - the real reason why the xml file was created
  • Markup elements surrounding the data

Here is an example


<student>
    <firstName>John</firstName></firstName>
    <lastName>Doe</lastName>
    <age>18</age>
</student>

The above XML markup is representing the data of a hypothetical student. This student has a first name of "John" and his last name is "Doe" and is "18" years old.

Data: The data represented here is "John", "Doe" and "18".

Markup elements: The markup elements are; student, firstName, lastName etc., Markup elements surround the data to add meaningful information about the data.

Tags and Elements in XML

XML tags are names surrounded by angle brackets. In XML you can invent your own tags.

A pair of starting and ending tags constitute an XML element. Every element should have a starting tag and an ending tag. Ending tag is same as starting tag except the element name starts with a forward slash /

A generic 'element' could be defined as:

<element>data</element>

All tags in XML are within angle brackets < and >.

If you start with a starting tag then, you have to end with an ending tag unless the element is empty. For empty elements you can skip writing the entire ending tag and instead close the starting tag itself. Here is an example

<element />

If one of the tags is missing or malformed then your XML file is not valid.

XML Attributes

An element can have optional attributes. In XML you can invent your own attributes also. An attribute is used to define a name-value pair.

Supposing you have two types of students; online and in-class and 'John Doe' is an online student then, you may add an attribute 'type=online' to the Student element as shown below


<student type="online">
    <firstName>John</firstName></firstName>
    <lastName>Doe</lastName>
    <age>18</age>
</student>

You can have an xml element with attributes but still be empty and there by not requiring a closing tag. Here is an example:


<student type="in-class" />

Why XML?

XML files could be created for many reasons. Here are a few:

  1. To send and receive data between two different parties - like two different companies, agencies, software systems etc..
  2. To feed a program a set of data that the program can consume and work with.

In this module you will not be using XML for the first reason mentioned above. You will be using XML file to send specific information to execute your Android program. In Android, your app specific data is sent to the Android API using XML files. When you create XML files in Android, you have to use the elements and attributes that the Android API can understand. Android developers have invented their elements and attributes and you need to use only those elements and attributes to create your Android app. You will learn more on those elements and attributes in the following chapter.

Quiz

Can you identify what is wrong with the xml element below?


<student type="online" />
    <firstName>John</firstName></firstName>
    <lastName>Doe</lastName>
    <age>18</age>
</student>

Reference

You can learn more on XML from: https://www.w3schools.com/xml/

results matching ""

    No results matching ""