We also don't need to use value to access the item's attribute value like we did before. One of the main differences is that the attrib object is simply a dictionary object, which makes it a bit more compatible with other Python code.
The result will be as follows: $ python treeparser.pyĪs you can see, this is very similar to the minidom example.
#Earmaster 7 import xml code
Using ElementTree, and like the previous code example, we obtain the node attributes and text using the objects related to each node. Once we have access to the root node we can easily traverse around the tree, because a tree is a connected graph. In our code we use the import command with the as keyword, which allows us to use a simplified name ( ET in this case) for the module in the code.įollowing the import, we create a tree structure with the parse function, and we obtain its root element. As always, in order to use it we must first import the module. Using ElementTreeĮlementTree presents us with an very simple way to process XML files. If we wanted to use an already-opened file, can just pass our file object to parse like so: datasource = open( 'items.xml')Īlso, if the XML data was already loaded as a string then we could have used the parseString() function instead. The result is as follows: $ python minidomparser.py Items = mydoc.getElementsByTagName( 'item') In the example below, we have accessed the attributes and text of a specific node, and of all nodes together. Since each node can be treated as an object, we can access the attributes and text of an element using the properties of the object. Thus, we can use the function getElementByTagName() to find a specific tag. The function returns a document, which can be handled as an XML type. Here the file name can be a string containing the file path or a file-type object. The parse function has the following syntax: (filename_or_file]) This module uses the parse function to create a DOM object from our XML file. In order to parse an XML document using minidom, we must first import it from the xml.dom module. However, it should be enough to demonstrate all of the XML operations in this article. In the examples below, we will be using the following XML file, which we will save as "items.xml": item1abc item2abc Īs you can see, it's a fairly simple XML example, only containing a few nested objects and one attribute. In this article, the ElementTree module will be used in all examples, whereas minidom will also be demonstrated, but only for counting and reading XML documents.
It is also likely a better candidate to be used by more novice programmers due to its simple interface, which you'll see throughout this article. The ElementTree module provides a more "Pythonic" interface to handling XMl and is a good option for those not familiar with the DOM. Thus, the use of this module requires that we are familiar with its functionality. The DOM is an application programming interface that treats XML as a tree structure, where each node in the tree is an object. The minidom, or Minimal DOM Implementation, is a simplified implementation of the Document Object Model (DOM). We'll also compare it with the older minidom module in the first few sections so you can get a good comparison of the two. Throughout this article we'll primarily take a look at the ElementTree module for reading, writing, and modifying XML data. With Python being a popular language for the web and data analysis, it's likely you'll need to read or write XML data at some point, in which case you're in luck. While not as common as it used to be, it is still used in services like RSS and SOAP, as well as for structuring files like Microsoft Office documents. XML, or Extensible Markup Language, is a markup-language that is commonly used to structure, store, and transfer data between systems.