You are here: Home > Dive Into Python > XML Processing > Parsing XML | << >> | ||||
Dive Into PythonPython from novice to pro |
As I was saying, actually parsing an XML document is very simple: one line of code. Where you go from there is up to you.
Example 9.8. Loading an XML document (for real this time)
>>> from xml.dom import minidom >>> xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml') >>> xmldoc <xml.dom.minidom.Document instance at 010BE87C> >>> print xmldoc.toxml() <?xml version="1.0" ?> <grammar> <ref id="bit"> <p>0</p> <p>1</p> </ref> <ref id="byte"> <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\ <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p> </ref> </grammar>
As you saw in the previous section, this imports the minidom module from the xml.dom package. | |
Here is the one line of code that does all the work: minidom.parse takes one argument and returns a parsed representation of the XML document. The argument can be many things; in this case, it's simply a filename of an XML document on my local disk. (To follow along, you'll need to change the path to point to your downloaded examples directory.) But you can also pass a file object, or even a file-like object. You'll take advantage of this flexibility later in this chapter. | |
The object returned from minidom.parse is a Document object, a descendant of the Node class. This Document object is the root level of a complex tree-like structure of interlocking Python objects that completely represent the XML document you passed to minidom.parse. | |
toxml is a method of the Node class (and is therefore available on the Document object you got from minidom.parse). toxml prints out the XML that this Node represents. For the Document node, this prints out the entire XML document. |
Now that you have an XML document in memory, you can start traversing through it.
Example 9.9. Getting child nodes
>>> xmldoc.childNodes [<DOM Element: grammar at 17538908>] >>> xmldoc.childNodes[0] <DOM Element: grammar at 17538908> >>> xmldoc.firstChild <DOM Element: grammar at 17538908>
Example 9.10. toxml works on any node
>>> grammarNode = xmldoc.firstChild >>> print grammarNode.toxml() <grammar> <ref id="bit"> <p>0</p> <p>1</p> </ref> <ref id="byte"> <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\ <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p> </ref> </grammar>
Example 9.11. Child nodes can be text
>>> grammarNode.childNodes [<DOM Text node "\n">, <DOM Element: ref at 17533332>, \ <DOM Text node "\n">, <DOM Element: ref at 17549660>, <DOM Text node "\n">] >>> print grammarNode.firstChild.toxml() >>> print grammarNode.childNodes[1].toxml() <ref id="bit"> <p>0</p> <p>1</p> </ref> >>> print grammarNode.childNodes[3].toxml() <ref id="byte"> <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\ <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p> </ref> >>> print grammarNode.lastChild.toxml()
Example 9.12. Drilling down all the way to text
>>> grammarNode <DOM Element: grammar at 19167148> >>> refNode = grammarNode.childNodes[1] >>> refNode <DOM Element: ref at 17987740> >>> refNode.childNodes [<DOM Text node "\n">, <DOM Text node " ">, <DOM Element: p at 19315844>, \ <DOM Text node "\n">, <DOM Text node " ">, \ <DOM Element: p at 19462036>, <DOM Text node "\n">] >>> pNode = refNode.childNodes[2] >>> pNode <DOM Element: p at 19315844> >>> print pNode.toxml() <p>0</p> >>> pNode.firstChild <DOM Text node "0"> >>> pNode.firstChild.data u'0'
<< Packages |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
Unicode >> |