Acord xml
lxml
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
Print to string
etree.tostring(doc)
Or you can print for human viewing
etree.tostring(doc,pretty_print=True)
Or if you want to print with method: xml,html,text do:
print etree.tostring(doc,method='text',pretty_print=True)
}
Root
Ger root element
root=etree.ElementTree(doc.getroot())
Find out the name of root element. First line is same as above.
root=doc.getroot() 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")Example
for i in root.findall(".//EffectiveDt"):
... print i.text
...
2009-05-14
None
Discover details of elements
print len(root.findall(".//Coverage"))