How to Create a Simple XML File
XML files are written in order to store information in a hierarchical fashion for later querying either by a database or information retrieval system. One of the greatest attractions of XML is that it is extremely flexible and can be molded by the system designer. For example, the tags are not predetermined as they are in HTML. The designer can label the tags however they see fit according to the nature of the task at hand. Although XML offers flexibility there are certain guidelines that must be followed in order for programs to run efficiently and logically. This article demonstrates how to create a single student entry for a grade book in XML.
Instructions
-
-
1
Create the declaration. Open a page in your text editor and type the following:
<?xml version=1.0?>
This is simply a declaration of what version of xml is being used. If you are using a later version you can write that in instead. -
2
Create the mother node. This is a broad description of the data. In this example the mother node will be called <gradebook>. So far the document should look like this:
<?xml version=1.0?>
<gradebook>
</gradebook>
Notice that you must close the tag with a '/' symbol. -
-
3
Create the root element. Our root element will be the student, <student>. The root element goes between the opening and closing tags of the mother node.
<?xml version=1.0?>
<gradebook>
<student>
</student>
</gradebook> -
4
Create nested elements and input information. Since the grade book keeps track of students' grades, add nested elements for the 'student name','the date of the test', 'the type of test', and 'the grade'. Input the appropriate information between each tag. The document should read as follows:
<?xml version=1.0?>
<gradebook>
<student>
<name>John Smith</name>
<date>March 1 2009</date>
<type>Oral exam</type>
<grade>95</grade>
</student>
</gradebook> -
5
Save the document. Go to 'save as' and save the document as "grades.xml". That's it. You have created a basic XML document. Now it is ready to be parsed by another program written in a more powerful language such as PERL or ASP.
-
1
Tips & Warnings
The declaration isn't necessary for all programs.
You are probably thinking that XML would be very monotonous to write, just the same old tags over and over again... However, the output should be done through an input interface that automatically prints out the tags as the information is saved. XML's utility is enhanced when it is utilized for other data driven programs.
Don't wait for XML to do anything. It is just a language used to organize data to be utilized by other programs. It is an excellent for this purpose but doesn't really do much of anything else.
Make sure to close your tags.
Make sure to write all in lower or upper case. Never use a mixture of both.
References
- Goldberg,K.H.(2009).Visual Quickstart Guide: XML. Berkeley:Peachpit Press