Differences between revisions 1 and 2
Revision 1 as of 2010-06-17 15:06:34
Size: 599
Comment: Initial start. read, print
Revision 2 as of 2010-06-17 16:56:12
Size: 1143
Comment: parsing and looping
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[TableOfContents(2)]] <<TableOfContents(2)>>
Line 29: Line 29:
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
}}}


== References ==
 1. http://codespeak.net/lxml/tutorial.html

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

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

References

  1. http://codespeak.net/lxml/tutorial.html

MyWiki: acordxml (last edited 2010-10-13 21:55:19 by LukaszSzybalski)