Acord xml


We will lxml to load and parse any xml data. You can install it on Debian/Ubuntu Linux using:

aptitude install python-lxml

Load acord xml file File

Load the file using lxml. This will create a doc element that will hold the whole structure of your file

from lxml import etree
doc = etree.parse ( 'PolicyNumberCompCollRequest.xml' )

How to navigate xml


Or you can print for human viewing


Or if you want to print with method: xml,html,text do:

print etree.tostring(doc,method='text',pretty_print=True)



Ger root element


Find out the name of root element. First line is same as above.

print root.tag

Loop through elements

for element in root.iter("*"):
    print element.tag,element.text,element.tail,element.attrib

Find elements

print root.find(".//Addr").tag
print root.find(".//Producer").tag
print root.find(".//InsuredOrPrincipal").tag
print root.find(".//PersPolicy").tag
print root.find(".//EffectiveDt").tag,root.find(".//EffectiveDt").text
print root.find(".//ExpirationDt").tag,root.find(".//ExpirationDt").text
print root.findall(".//EffectiveDt")
print root.findall(".//Coverage")


for i in root.findall(".//EffectiveDt"):
...     print i.text

Lookup definitions:

nodename Selects all child nodes of the named node
/        Selects from the root node
//       Selects nodes in the document from the current node that match the selection no matter where they are
.        Selects the current node
..       Selects the parent of the current node
@        Selects attributes

bookstore       Selects all the child nodes of the bookstore element
/bookstore      Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book  Selects all book elements that are children of bookstore
//book          Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
//@lang         Selects all attributes that are named lang

Discover details of elements

print len(root.findall(".//Coverage"))