Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

4.7 Extracting Elements from an XML File

Each element in an XML document has a name. To extract an element you ask for all elements that match a given name. For the drawing application's XML document format we start by getting the GraphicsCommands element.

graphicsCommands =

xmldoc . getElementsByTagName (" GraphicsCommands ")

The code above returns a list of all elements at the top-level of the document that match the tag name GraphicsCommands. We know there is only one of these elements in the file, so we can get just the first one by using index 0 into the list.

graphicsCommand =

xmldoc . getElementsByTagName (" GraphicsCommands " )[0]

The graphicsCommand variable is set to the first, and only, of the matching DOM elements returned by the minidom parser. DOM stands for Document Object Model. Now that we have the graphicsCommand element we can get sub-elements from it. The sub-elements of it are the list of Command elements.

commands = graphicsCommand . getElementsByTagName (" Command ")

Finally, if we wish to draw the picture stored in the file, we can traverse the Command

elements with a for loop.

for command in commands :

# Draw the command on the screen .

# This code is omitted for now .

4.8 XML Attributes and Dictionaries

In the XML file presented in Example 4.7 many of the Command elements have attributes. For instance, the BeginFill command has a color attribute. The GoTo command on lines 4–5 has attributes x, y, width, and color. These attributes provide information about each of their graphics commands. The attribute names of x, y, width, and color are called the attribute keys and their values are the strings to which each key is assigned.

To correctly draw the picture in one of these picture XML files, we must be able to access the attributes of a graphics command and use them when drawing the picture. It is possible to access the attributes of an XML element through an attributes dictionary.

A dictionary is a little like a list. You can use indexing to look up values within the dictionary just like you use indexing to look up values within a list. The difference is that instead of using only integers as the index values, you can use any value you like. To lookup an attribute in the attributes dictionary we use its key.

Example 4.8 A list and a dictionary have similarities. Both data types hold a collection of values. The difference between a list and a dictionary are in the values used to index into them. In a list, the index values must be non-negative integers and the locations within the list are numbered sequentially starting at 0.

Within a dictionary there is no ordering of the index values. An index value, called a key when working with dictionaries, can be nearly any value. A dictionary is a list of key, value pairs. Each key is mapped to a value. Keys must be unique, values do not have to be unique in the dictionary.

Here is some code that creates both a list and a dictionary and demonstrates similar operations on the two datatypes.

1 lst = [] # an empty list

2 dct = {} # an empty dictionary


4 # The append method adds items to a list.

5 lst.append("Biking")

6 lst.append("Running")

7 lst.append("Other")


9 # The next line adds Sport/Running as a key/value pair

10 dct["x"] = "299.0"

11 dct["y"] = " -45.0"

12 dct["color"] = "#804000"


14 # We can iterate over a list by using a for loop.

15 f o r i i n r a n g e ( l e n (lst )):

16 p r i n t (i, lst[i])


18 # We can iterate over a dictionary using a for loop

19 # to go through the list of keys to the dictionary.

20 f o r key i n dct.keys ():

21 p r i n t (key , dct[key])

The output when this code is executed is as follows.

0 Biking

1 Running

2 Other

y -45.0

x 299.0

color #804000

Chapter 12 contains a complete listing of dictionary operators and methods.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science