Вопрос

I'm trying to find out how to retrieve XML attribute values using the cElementTree iterparse in Python (2.7). My XML is something like this:

<root>
  <record attr1="a" attr2="b" attr3="c" ... />
  <record attr1="x" attr2="y" attr3="z" ... />
  ...
</root>

My code is something like this:

context = ET.iterparse(sys.stdin, events=('start','end'))
context = iter(context)
event, root = context.next()

for event, elem in context:
    if event == 'end' and elem.tag == 'record':
        # get the attributes!! 
    elem.clear()
    root.clear()

I'm dealing with big data from stdin. I'm not having any luck figuring this out. Could someone please tell how (optimal?) to do this?

Это было полезно?

Решение

Oops, staring me in the face, sort of: http://docs.python.org/2/library/xml.etree.elementtree.html#element-objects

In summary:

elem.get('attr_name', default_value)

or,

for name, value in elem.items():

or,

elem.attrib() - dictionary

I should also add that it was necessary to modify the code posted in the question:

...
if event == 'start' and elem.tag == 'record':
... 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top