Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

1.15 XML Files

Reading a standard text file, like the graphics commands file we read using the loop and a half pattern in Sect. 1.13.2, is a common task in computer programs. The only problem is that the program must be written to read the specific format of the input file. If we later wish to change the format of the input file to include, for example, a new option like fill color for a circle, then we are stuck updating the program and updating all the files it once read. The input file format and the program must always be synchronized. This means that all old formatted input files must be converted to the new format or they must be thrown away. That is simply not acceptable to most businesses because data is valuable.

To deal with this problem, computer programmers designed a language for describing data input files called XML which stands for eXtensible Markup Language. XML is a meta-language for data description. A meta-language is a language for describing other languages. The XML meta-language is universally accepted. In fact, the XML format is governed by a standards committee, which means that we can count on the XML format remaining very stable and backwards compatible forever. Any additions to XML will have to be compatible with what has already been defined.

An XML document begins with a special line to identify it as an XML file. This line looks like this.

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

The rest of an XML file consists of elements or nodes. Each node is identified by a tag or a pair of beginning and ending tags. Each tag is delimited (i.e. surrounded) by angle brackets. For instance, here is one such tag.

<GraphicsCommands>

Most XML elements are delimited by a opening tag and a closing tag. The tag above is an opening tag. Its matching closing tag looks like this.

</GraphicsCommands>

The slash just before the tag name means that it is a closing tag. An opening and closing tag may have text or other XML elements in between the two tags so XML documents may contain XML elements nested as deeply as necessary depending on the data you are trying to encode.

Each XML element may have attributes associated with it. For instance, consider an XML element that encapsulates the information needed to do a GoTo graphics command. To complete a GoTo command we need the x and y coordinates, the width of the line, and the pen color. Here is an example of encoding that information in XML format.

<Command x="1.0" y="1.0" width="1.0" color="#000000">GoTo</Command>

In this example the attributes are x, y, width, and color. Each attribute is mapped to its value as shown above. The GoTo text is the text that appears between the opening and closing tags. That text is sometimes called the child data.

By encoding an entire graphics commands input file in XML format we eliminate some of the dependence between the Draw program and its data. Except for the XML format (i.e. the grammar) the contents of the XML file are completely up to the programmer or programmers using the data. The file in Sect. 1.15.1 is an example of the truck picture's XML input file.

1.15.1 The Truck XML File

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

2 <GraphicsCommands>

3 <Command color="black">BeginFill</Command>

4 <Command radius="20.0" width="1" color="black">Circle</Command>

5 <Command>EndFill</Command>

6 <Command>PenUp</Command>

7 <Command x="120.0" y="0.0" width="1.0" color="black">GoTo</Command>

8 <Command>PenDown</Command>

9 <Command color="black">BeginFill</Command>

10 <Command radius="20.0" width="1" color="black">Circle</Command>

11 <Command>EndFill</Command>

12 <Command>PenUp</Command>

13 <Command x="150.0" y="40.0" width="1.0" color="black">GoTo</Command>

14 <Command>PenDown</Command>

15 <Command color="yellow">BeginFill</Command>

16 <Command x="-30.0" y="40.0" width="1.0" color="black">GoTo</Command>

17 <Command x="-30.0" y="70.0" width="1.0" color="black">GoTo</Command>

18 <Command x="60.0" y="70.0" width="1.0" color="black">GoTo</Command>

19 <Command x="60.0" y="100.0" width="1.0" color="black">GoTo</Command>

20 <Command x="90.0" y="100.0" width="1.0" color="black">GoTo</Command>

21 <Command x="115.0" y="70.0" width="1.0" color="black">GoTo</Command>

22 <Command x="150.0" y="70.0" width="1.0" color="black">GoTo</Command>

23 <Command x="150.0" y="40.0" width="1.0" color="black">GoTo</Command>

24 <Command>EndFill</Command>

25 </GraphicsCommands>

XML files are text files. They just contain extra XML formatted data to help standardize how XML files are read. Writing an XML file is as simple as writing a text file. While indentation is not necessary in XML files, it is often used to highlight the format of the file. In Sect. 1.15.1 the GraphicsCommands element contains one Command element for each drawing command in the picture. Each drawing command contains the command type as its text. The command types are GoTo, Circle, BeginFill, EndFill, PenUp, and PenDown. The attributes of a command are data like x, y, width, radius, and color that are used by the various types of commands.

To write the commands to a file, each of the Command classes can be modified to produce an XML element when converted to a string using the special str method. For instance, Sect. 1.15.2 contains the modified GoToCommand class supporting the creation of an XML element.

1.15.2 The GoToCommand with XML Creation Code

1 # The following classes define the different commands that

2 # are supported by the drawing application.

3 class GoToCommand:

4 def init (self,x,y,width=1,color="black"):

5 self.x = x

6 self.y = y

7 self.width = width

8 self.color = color

9

10 # The draw method for each command draws the command

11 # using the given turtle

12 def draw(self,turtle):

13 turtle.width(self.width)

14 turtle.pencolor(self.color)

15 turtle.goto(self.x,self.y)

16

17 # The str method is a special method that is called

18 # when a command is converted to a string. The string

19 # version of the command is how it appears in the graphics

20 # file format.

21 def str (self):

22 return '<Command x="' + str(self.x) + '" y="' + str(self.y) + '" width="' +

23 str(self.width) + '" color="' + self.color + '">GoTo</Command>'

By returning a string like this from each of the command objects, the code to write the draw program's data to a file is very simple. All that is needed is some code that writes the xml line as the first line, followed by the <GraphicsCommands> tag and the command elements. Finally, the <GraphicsCommands> tag must be written. The code in Sect. 1.15.3 accomplishes this.

1.15.3 Writing Graphics Commands to an XML File

1 file = open(filename, "w")

2 file.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?> ')

3 file.write('<GraphicsCommands> ')

4 for cmd in self.graphicsCommands:

5 file.write(' '+str(cmd)+" ")

6

7 file.write('</GraphicsCommands> ')

8 file.close()

Writing an XML file is like writing any text file except that the text file must conform to the XML grammar specification. There are certainly ways to create XML files that differ from how it was presented in Sect. 1.15.3. In the next section we'll learn about XML parsers and a very simple way to read XML documents. It turns out there are at least some XML frameworks that make writing an XML document just as simple.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel