Next / Previous / Contents / TCC Help System / NM Tech homepage

3. Reading an XML document

Suppose you want to extract some information from an XML document. Here's the general procedure:

  1. You'll need to import the lxml package. Here is one way to do it:

    from lxml import etree
    

  2. Typically your XML document will be in a file somewhere. Suppose your file is named test.xml; to read the document, you might say something like:

    doc = etree.parse('test.xml')
    

    The returned value doc is an instance of the ElementTree class that represents your XML document in tree form.

    Once you have your document in this form, refer to Section 8, “class ElementTree: A complete XML document” to learn how to navigate around the tree and extract the various parts of its structure.

For other methods of creating an ElementTree, refer to Section 7, “Features of the etree module”.