About Me

Friday, 13 April 2012

XML Syntax

XML Syntax

If you have already programmed in HTML, XML syntax shouldn't be that much of a leap to grasp. While there are a couple of new items in XML, it is still just a markup language that relies on tags to get the job done.

XML Syntax: Our Example XML Document

This lesson will analyze a real XML file to point out the syntax used to create a Well-Formed XML document. This XML document was used to keep track of students in an Computer Application's Course.

XML Code:

<?xml version="1.0" encoding="ISO-8859-15"?>
<class_list>
 <student>
  <name>Robert</name>
  <grade>A+</grade>
 </student>
 <student>
  <name>Lenard</name>
  <grade>A-</grade>
 </student>
</class_list>

If you spend a few moments reading through the above XML document, you can see it has two students, Robert and Lenard. This simple XML document readily displays the benefits of XML. The data seems to almost describe itself, which is one of the main reasons XML was created!
Let's take a look at XML syntax by looking at the different parts of this document. Note: This is just an overview of XML's syntax and there are links to more information provided in this lesson.

XML Prolog (optional)

I think the only thing not easily understandable in our XML document is that first line with all the symbols and attributes. This rather confusing line in our XML document is referred to as the XML Prolog.

XML Code:

<?xml version="1.0" encoding="ISO-8859-15"?>
This line with special symbols and attributes is what is referred to as the XML declaration. It simply states what version of XML you are using and the type of encoding you are using. Don't worry too much about this line just always include it exactly as we have above. If you are curious, feel free to learn more about ISO 8859.

XML Tag

The XML tag is very similar to that of the HTML tag. Each element is marked with an opening tag and a closing tag. Below, we have highlighted both the opening and closing tags from a piece of our example XML file.

XML Code:

<name>Robert</name>
<grade>A+</grade>
Later in the tutorial, we talk more about XML Tags, including opening tags, closing tags, and all the text inbetween them, which combine to form XML Elements.

XML Elements

XML Elements are the meat and potatoes of XML. They contain the opening and closing tags, child elements, and data. In our XML document the student element is comprised of all of the following information. Notice how it has a couple of child elements.

XML Code:

<student>
 <name>Robert</name>
 <grade>A+</grade>
</student>

0 comments:

Post a Comment