Using XML files – PLIST and others

As we noted in Chapter 10, Serializing and Saving – JSON, YAML, Pickle, CSV, and XML, Python's xml package includes numerous modules that parse the XML files. Because of the wide adoption of the XML files, it often becomes necessary to convert between XML documents and Python objects. Unlike JSON or YAML, the mapping from XML is not simple.

One common way to represent the configuration data in XML is the PLIST file. For more information on the PLIST format, you can refer to https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

Macintosh users with XCode installed can perform man plist to see extensive documentation on the XML-based format. The advantage of the PLIST format is that it uses a few, very general tags. This makes it easy to create PLIST files and parse them. Here's the sample PLIST file with our configuration parameters:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
  <key>player</key> 
  <dict> 
    <key>betting</key> 
    <string>Flat</string> 
    <key>play</key> 
    <string>SomeStrategy</string> 
    <key>rounds</key> 
    <integer>100</integer> 
    <key>stake</key> 
    <integer>50</integer> 
  </dict> 
  <key>simulator</key> 
  <dict> 
    <key>outputfile</key> 
    <string>ch14_simulation6a.dat</string> 
    <key>samples</key> 
    <integer>100</integer> 
  </dict> 
  <key>table</key> 
  <dict> 
    <key>dealer</key> 
    <string>Hit17</string> 
    <key>decks</key> 
    <integer>6</integer> 
    <key>limit</key> 
    <integer>50</integer> 
    <key>payout</key> 
    <array> 
      <integer>3</integer> 
      <integer>2</integer> 
    </array> 
    <key>split</key> 
    <string>NoResplitAces</string> 
  </dict> 
</dict> 
</plist> 

Here, we're showing you the nested dictionary-of-dictionary structure in this example. There are a number of Python-compatible types encoded with XML tags:

Python type

Plist tag

str

<string>

float

<real>

int

<integer>

datetime

<date>

boolean

<true/> or <false/>

bytes

<data>

list

<array>

dict

<dict>

 

As shown in the preceding example, the dict <key> values are strings. This makes the PLIST file a very pleasant encoding of our parameters for our simulation application. We can load a PLIST-compliant XML file with relative ease:

import plistlib
print(plistlib.load(plist_file))

This will reconstruct our configuration parameter from the XML serialization. We can then use this nested dictionary-of-dictionaries structure with the main_nested_dict() function shown in the preceding section on JSON configuration files.

Using a single module function to parse the file makes the PLIST format very appealing. The lack of support for any customized Python class definitions makes this equivalent to JSON or a properties file. 

In addition to the standardized PLIST schema, we can define our own customized schema. In the next section, we'll look at creating XML that's unique to our problem domain.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset