XML Example

Overview

XML is really all about storing data. Let's use the class as example. Say I want to store information about the class in a XML document. I may create a structure like

<class>
	<student>
		<name>John Doe</name>
		<email>jdoe@ccbcmd.edu</email>

		<grade>B</grade>
	</student>
	<student>
		<name>Mary Jane</name>
		<email>Mjane@ccbcmd.edu</email>

		<grade>B</grade>
	</student>
</class>

Notice the root element of class. All XML document require a root element that will house all other elements. In this example I can continue to add students to this class. If I wanted to store information about another class I would create another XML file with the root class.

Viewing XML Documents

Click here to view the class.xml file. In most browsers you should see the simple XML tree structure. You can collapse elements . This is a simple way to test your XML files. Web Browsers will typically display an error message and refuse to load the page if it is not well formed.

Viewing XML documents in a browser is useful while developing and testing but most XML documents will be processed by an application.

XML and CSS

For a simple way to format an XML document you can apply CSS styles just like you would with HTML or XHTML.

With CSS you will be using the element names from the XML document as selector names. For example...

class  {
  background-color:#efefef;
	border:2px solid #009;
}
student  {
	display:block;
	text-align:center;
}
name  {
	background-color:#330099;
	display:block;
}

You would then attach the style sheet using a basic style sheet processing instruction.

<?xml-stylesheet type="text/css" href="studentDisplay.css"?>

Take a look

Click here to see an XML file style with a CSS file.

Click here to see the CSS file.