XML Basics

XML stands for eXtensible Markup Lanuage. This text based tag notation allows data to be effectively labeled and easily passed, written and read. It is also human readable, making it easy for non-programmers to update information. Common uses for XML include RSS feeds and AJAX.

XML Syntax

  1. XML Declaration: All XML data files need to start with the document declaration < ?xml version='1.0' ? >. Notice that this is written in all lowercase and there are quotes around the attribute value. In this way, XML is very much like HTML.
  2. Root Element: Similar, to HTML, each XML document has to have a root element. In HTML, the root element is <html>. In XML, the root element is used to describe the data that will occur in the page. All tags and information in the page MUST be contained inside the root element. If we were making a document that stored a student schedule, our root element might be <schedule>...</schedule>.
  3. Childe Elements: It is important to note, that while XML tags are written following the same rules as HTML tags, the major difference is that the data designers get to pick the names of the tags in XML. The main goal is to choose names that clearly identify the content. Element names must start with a letter and have no spaces. Like most identifiers in programming, it is best to stick to letters, numbers and underscores. Be sure to place the content between the opening and closing tags of an element. Also, remember that CasE MAtTeRs and that no element names can begin with 'xml'.
  4. Nesting Elements: XML structures, like HTML, can be as complex as necessary. This means that you can nest elements inside of elements to create objects. It is important to nest items properly, meaning the inner tags must end before the outer tags.
  5. Attributes: XML allows the creation of attributes on a tag to stroes additional information without adding text to the content itself. Attributes in XML are written in name/value pairs. Names must be all lowercase and values should be enclosed in quotes. In introductory versions of XML, the use of attributes is discouraged as there is no way to process their values.
  6. Empty Elements: Empty elements are elements with no content. Typically, the information associated with an empty element uses attributes and often points to a file. Empty elements have no closing tag and use /> to denote the end of the tag.
  7. Comments: Comments in XML are the same as in HTML.


Data Design

When given a big project with a lot of data, one of the first steps is to determine all of the variables that are needed and break them up into objects. Again, the ‘has-a’ test is a key element in doing to. For example, if I am given the task of creating a program that will keep the signup information for students interested in enrolling in a preschool, I first need to understand the information that will be gathered.

So, we decide to gather the following information: names of the parent and student(s), address, phone, email, student bday, student age, application fee paid?, class assigned. In looking at the information, we see two main objects: Parent and Student. So, we design the following:
We should also realize that there will be a list of parent desiring enrollment and that each parent may have a list of kids. So, the main array will be of type parent and then each parent will have a possible list of students. The resulting code example might look like this: < ?xml version='1.0' ? > <enrollment> <parent> <name>Jane Smith</name> <address>123 My Drive</address> <phone>123-456-7890</phone> <email>me@home.com</email> <student> <name>Zoe Smith</name> <bday>09/17/2012</bday> <age>3</age> <class>PreK 3</class> <feePaid>true</feePaid> </student> <student> <name>Jimmy Smith</name> <bday>05/05/2013</bday> <age>2</age> <class>PreK 2</class> <feePaid>false</feePaid> </student> </parent> </enrollment>

Basic Display

XML is most often processed using AJAX and/or XSLT. However, at the most basic level, since XML is formed using tags, the data in an XML document can be displayed using CSS. All information will appear on the page in the order in which it was written. Accessing this style will require the following command: < ?xml-stylesheet type='text/css' href='style.css' ? >