XML Processing with Python: Part Seven

XML processingThe following Python SAX and DOM parsers are available: xml.sax and xml.dom.minidom. Here is an overview of xml.dom.

xml.dom.minidom is a lightweight DOM implementation, designed to be simpler and smaller than a full DOM implementation.

Converting from XML to DOM and Back

In the following example, we’ll use the example XML file from the first article in this series, which we’ll save as mlb.xml:

<?xml version=”1.0″?>
<mlb>
<team>
<name>New York Mets</name>
<generalmanager>Sandy Alderson</generalmanager>
</team>
<team>
<name>Washington Nationals</name>
<generalmanager>Mike Rizzo</generalmanager>
</team>
<team>
<name>Atlanta Braves</name>
<generalmanager>John Hart</generalmanager>
</team>
</mlb>

Then we’ll enter the following code into our Python interpreter:

from xml.dom.minidom import parse

from xml.dom.minidom import parse

def printMLB(mlb):
    teams = mlb.getElementsByTagName("team")
    for team in teams:
        print("*****Team*****")
        print("Name: %s"  % team.getElementsByTagName("name")[0].childNodes[0].data)
        for gm in team.getElementsByTagName("generalmanager"):
            print("General manager: %s" % gm.childNodes[0].data)
# open an XML file and parse it into a DOM
myDoc = parse('mlb.xml')
myMLB = myDoc.getElementsByTagName("mlb")[0]
# Get all the team elements in the library
teams = myMLB.getElementsByTagName("team")
# Print each team's name and general manager
printMLB(myMLB)
# Insert a new team in the list
newTeam = myDoc.createElement("team")
newTeamName = myDoc.createElement("name")
teamNameText = myDoc.createTextNode("Miami Marlins")
newTeamName.appendChild(teamNameText)
newTeam.appendChild(newTeamName)
newGeneralManager = myDoc.createElement("generalmanager")
generalManager = myDoc.createTextNode("Dan Jennings")
newGeneralManager.appendChild(generalManager)
newTeam.appendChild(newGeneralManager)
myMLB.appendChild(newTeam)
print("Added a new team!")
print("##########################")
printMLB(myMLB)
# Remove a team from the list
# Find New York Mets
for team in myMLB.getElementsByTagName("team"):
    for name in team.getElementsByTagName("name"):
        if name.childNodes[0].data.find("New York Mets") != -1:
            removedTeam = myMLB.removeChild(team)
            removedTeam.unlink()
print("Removed a team.")
print("##########################")
printMLB(myMLB)
# Write back to the XML file
mlb = open("mlb.xml", "w")
mlb.write(myMLB.toprettyxml(" "))
mlb.close()

To create a DOM, the document needs to be parse into a document tree. This is done by calling the parse method from xml.dom.mindom. This method returns a Document object, which contains methods for querying for child nodes, getting all nodes in the document of a certain name, and creating new nodes, among other things. The getElementsByTagName method returns a list of node objects whose names match the argument, which is used to extract the root node of the document: the <mlb> node. The print method uses getElementsByTagName again, and then for each team node, prints the name and general manager. nodes with text that follows them are considered to have a single child node, and the text is stored in the data attribute of that node, so team.getElementsByTagName(“name”)[0].childNodes[0].data simply retrieves the text node below the <name> element and returns its data as a string.

Constructing a new node in DOM requires creating a new node as a piece of the Document object, adding all necessary attributes and child nodes, and then attaching it to the correct node in the document tree. The createElement(tagName) method of the Document object correct node in the document tree. The createElement(tagName) method of the Document object creates a new node with a tag name set to whatever argument has been passed in. adding text nodes is accomplished almost the same way, with a call to createTextNode(string). When all the nodes have been created, the structure is created by calling the appendChild method of the node to which the newly created node will be attached. Node also has a method called insertBefore(newChild, refChild) for inserting nodes in an arbitrary location in the list of child nodes, and replaceChild(newChild, oldChild) to replace one node with another.

Removing nodes requires first getting a reference to the node being removed and then a call to removeChild(childNode). After the child has been removed, it’s advisable to call unlink() on it to force garbage collection for that node and any children that may still be attached. This method is specific to the minidom implementation and and is not available in xml.dom.

Finally, having made all these changes to the document, it would be a good idea to write the DOM back to the file from which it came. A utility method is included with xml.dom.minidom called toprettyxml, which takes two optional arguments: an indentation string and a newline character. If not specified, these default to a tabulator and \n, respectively. This utility prints a DOM as nicely indented XML and is just the thing for printing back to the file.

Note that in the program, we added one team (Miami Marlins) and deleted one team (New York Mets). The program should produce the following output:

*****Team*****
Name: New York Mets
General manager: Sandy Alderson
*****Team*****
Name: Washington Nationals
General manager: Mike Rizzo
*****Team*****
Name: Atlanta Braves
General manager: John Hart
Added a new team!
##########################
*****Team*****
Name: New York Mets
General manager: Sandy Alderson
*****Team*****
Name: Washington Nationals
General manager: Mike Rizzo
*****Team*****
Name: Atlanta Braves
General manager: John Hart
*****Team*****
Name: Miami Marlins
General manager: Dan Jennings
Removed a team.
##########################
*****Team*****
Name: Washington Nationals
General manager: Mike Rizzo
*****Team*****
Name: Atlanta Braves
General manager: John Hart
*****Team*****
Name: Miami Marlins
General manager: Dan Jennings

After running the program, I discovered that toprettyxml inserts extra whitespaces. Therefore, it might be better if we used toxml() (which doesn’t insert extra whitespaces), or use something else entirely such as xml.dom.ext.PrettyPrint, as described in this blog article.

External Links:

Download page for PyXML – an XML parser with a better solution than toprettyxml (PrettyPrint)

Wikipedia page on DOM

XML Processing with Python: Part Six

XML ProcessingDOM (Document Object Model)

At the heart of DOM lies the Document object. This is a tree-based representation of the XML document. Tree-based models are a natural fit for XML’s hierarchical structure, making this a very intuitive way of working with XML. Each element in the tree is called a Node object, and it may have attributes, child nodes, text, and so forth, all of which are also objects that are stored in the tree. DOM objects have a number of methods for creating and adding nodes, for finding nodes of a specific type or name, and for reordering or deleting nodes.

Differences between SAX and DOM

The major difference between SAX and DOM is DOM’s ability to store the entire document in memory and manipulate and search it as a tree, rather than force you to parse the document repeatedly, or force you to build your own in-memory representation of the document. The document is parsed once, and then nodes can be added, removed, or changed in memory and then written back out to a file when the program is finished.

Although either SAX or DOM can do almost anything you might want to do with XML, you might want to use one over the other in certain circumstances. For instance, if you are working on an application in which you will be modifying an XML document repeatedly based on user input, you might want the convenient random access capabilities for DOM. but if you are building an application that needs to process a stream of XML quickly with minimal overhead, SAX might be a better choice for you.

DOM is designed with random access in mind. It provides a tree that can be manipulated at runtime and needs to be loaded into memory only once. SAX is stream-based, so data comes in as a stream one character after the next, but the document isn’t seen in its entirety before it starts getting processed; therefore, if you want to randomly access data, you have to either build a partial tree of the document in memory based on document events, or reparse the document every time you want a different piece of data.

Most people find the object-oriented behavior of DOM very intuitive and easy to learn. The event-driven model of SAX is more similar to functional programming and can be more challenging to get up to speed on.

If you are working in a memory-limited environment, DOM is probably not a good choice. Even on a fairly high-end system, constructing a DOM tree for a large document (say 2-3 MB) can bring the computer to a halt while it processes. Because SAX treats the document as a stream, it never loads the whole document into memory, so it is preferable if you are memory constrained or working with very large documents.

Using DOM requires a great deal of processing time while the document tree is being built, but once the tree is built, DOM allows for much faster searching and manipulation of nodes because the entire document is in memory. SAX is somewhat fast for searching documents, but not as efficient for their manipulation. However, for document transformations, SAX is considered to be the parser of choice because the event-driven model is fast and very compatible with how XSLT works.

In the next article, we’ll look at SAX and DOM parsers for Python.

External Links:

XML DOM Parser at W3Schools

Document Object Model at Wikipedia