Create an XML File

Free HTML5 Game Tutorial for Beginners: Page 18

Overview Advantages Create an XML File Header Root Lists inside XML One Question Entire XML File Summary
XML File Icon

Overview

This article explains how to create an XML file. The example includes how to add the header, root, list elements, and elements with values. This tutorial describes elements and PCDATA values. This article discusses advantages of XML files. This lesson includes a link to view the entire contents of an XML file.

The XML file structure is based on a document type definition (DTD) file. See the Create a Document Type Definition (DTD) tutorial for more information about DTD files.

Advantages

XML files work great for storing information about games and other applications.

Most games have levels. The Surface Game example loads an XML file for each level. A separate XML file contains the set of questions, answers, and image file paths, for each level. To add more levels create more graphics and another XML file.

XML files allow configurable applications. Configurable applications are easily modified to suit the needs of the user. The best configurable applications require minimal changes to source code. For example change the Surface Game with modifications to XML files and graphics alone.

Configurable applications decrease development time. For example, assume a game's engine and interface required hours of design and development time. Players love the game and want more levels. With properly prepared source code, the developer simply adds more XML files. There's no need to modify, test, and debug code.

Additionally XML files are stored on the server. Only information for the current level needs to download in order to play the game. The game can download quickly and the user can start playing, when the current level has loaded. If the information for all levels were stored together, then the game would take longer to initialize.

An XML file can be considered part of the model in the Model-View-Controller Design Pattern (MVC).

How to Create an XML File

XML files are simple text files. They can be edited with any text editor. The free Eclipse for PHP Developers application works great. Eclipse highlights most errors in an XML file. Therefore the task of creating an XML file is quicker with Eclipse.

XML files store information in a structured format. XML is an abbreviation for eXtensible Markup Language. Extensible for XML, means developers can add (Extend) their own tag names to the XML file. Markup means the file is defined by tags rather than source code. A tag is a property with a name that's declared with opening and closing brackets.

For example the tag named ql is declared with opening brackets as <ql> and closing brackets as </ql>. The following line demonstrates an empty ql element, without attributes or values.

<ql></ql>

XML files consist of element tags, and attributes. Attributes consist of name value pairs, within an element's opening tag. Attributes aren't recommended. This tutorial focuses on elements only.

Header

The XML file's first line declares ths XML version and character encoding. The Surface Game's XML files start with the following line.

<?xml version="1.0" encoding="UTF-8"?>	
		

The XML file's second line includes a reference to the DTD file. The name of the Surface Game's DTD is levels.dtd. The name of the root element called lvl appears after DOCTYPE in the header's second line. All element names such as lvl, ql, q, a, and g, are declared in the a DTD file. See the Create a Document Type Definition (DTD) tutorial for details. The following line demonstrates the second line in the Surface Game's XML files.

<!DOCTYPE lvl SYSTEM "levels.dtd">

Root

Each XML file contains one element called the root element. All other elements are inside or contained within the root element.

The Surface Game example declares the name lvl for the root element. Name lvl stands for one level of the Surface Game. Each XML file contains the data for one level of the game.

The root element name appears three times in an XML file. First, the root element name appears following the DOCTYPE declaration in the header. Second, the opening root element tag appears directly under the header. Third, the closing root element appears at the very end of the XML document. See the full XML file for an example. Notice tags begin with <lvl> and end with </lvl>.

Lists

The Surface Game includes a list of questions for each level. The Create a Document Type Definition (DTD) tutorial demonstrated declaring element ql with DTD. Element ql is an abbreviation for question list.

Three more elements appear inside each ql element. Those three elements are q,a, and g.

Element q represents one question. Element a represents one answer. Element g represents the string path to one graphic.

One Question

One question in the XML file includes each sub tag q, a, and g, in order. The example below shows all the data for one question. Note the ql opening and closing tags at the top and bottom. Within the ql tag include q, a, and g, opening and closing tags.

The text for one question is in between the q opening and closing tags. The text for one answer is in between the a opening and closing tags. The text which points to the source file of one graphic, is in between the g opening and closing tags.

The text between the opening and closing tags of the q,a, and g tags can be referred to as the value of each element respectively. Text values are declared as PCDATA. Elements can contain different types of values. PCDATA stands for Parsed Character data. PCDATA is textual data ready for interpretation. The Parse XML with JavaScript tutorial explains the process which reads data from an XML file. The following listing demonstrates one question's declaration in an XML file.

<ql>

<q>
Find the area of the square. 
</q>
	
<a>
529
</a>

<g>
l1q1.gif
</g> 
	 			
</ql>		
		

Entire File

The entire list of questions for one level in an XML file includes a set nested within ql elements. Each ql contains exactly one q,a, and g element.

Open 1.xml to see the questions and answers for level one. See the Parse XML with JavaScript tutorial, to learn how to access XML properties programmatically.

Summary

This article explained how to create an XML file. The example included how to add the header, root, list elements, and elements with values. This tutorial described elements and PCDATA values. This article discussed advantages of XML files. This lesson included a link to view the entire contents of an XML file.

The XML file structure is based on a document type definition (DTD) file. See the Create a Document Type Definition (DTD) tutorial for more information about DTD files.

See the Parse XML with JavaScript tutorial, to learn how to use XML in a game. For the next tutorial, tap the image icon containing text which says Next Lesson, or the right pointing arrow at the bottom of the page.

Previous Page Next Page
Copyright © 2015 Seven Thunder Software. All Rights Reserved.