brucezepplin
brucezepplin

Reputation: 9792

python : get subelements from elements define in elementTree using a for loop

If I have the following xml:

<uima.cas.FSArray _id="7429" size="2">
<i>7409</i>
<i>7419</i>
</uima.cas.FSArray>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7342" codingScheme="SNOMEDCT" code="269435009" oid="269435009#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7322" codingScheme="SNOMEDCT" code="157754004" oid="157754004#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7352" codingScheme="SNOMEDCT" code="269432007" oid="269432007#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7332" codingScheme="SNOMEDCT" code="213029005" oid="213029005#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7362" codingScheme="SNOMEDCT" code="157762007" oid="157762007#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<uima.cas.FSArray _id="7372" size="5">
<i>7362</i>
<i>7332</i>
<i>7352</i>
<i>7322</i>
<i>7342</i>
</uima.cas.FSArray>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7235" codingScheme="SNOMEDCT" code="274241003" oid="274241003#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7265" codingScheme="SNOMEDCT" code="39384006" oid="39384006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7255" codingScheme="SNOMEDCT" code="206825002" oid="206825002#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7275" codingScheme="SNOMEDCT" code="20262006" oid="20262006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7245" codingScheme="SNOMEDCT" code="158202006" oid="158202006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<uima.cas.FSArray _id="7285" size="5">
<i>7245</i>
<i>7275</i>
<i>7255</i>
<i>7265</i>
<i>7235</i>

I want to get the _id and the <i> subelements from nodes uima.cas.FSArray

i.e. looking at the first node (first three lines) I would like to retrieve something like

_id    i
7429   7409
7429   7419

and similar for following uima.cas.FSArray nodes.

I realize that the same node (without attributes appears) so I am only interested in nodes with the _id element.

Here is my attempt:

#!/usr/bin/env python

import sys
import os
import xml.etree.cElementTree as ET

tree = ET.ElementTree(file=sys.argv[-1])

UMLSarr = {}
for x in tree.iterfind('uima.cas.FSArray'):
    UMLSarr[x] = x.attrib
    subArr[x] = SubElement(UMLSarr[x],"subArr",attrib='i')

but I get:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
NameError: name 'SubElement' is not defined

I've tried various other iterations of this code but I'm running into more and more errors and hoped someone could give me a hand.

Thanks.

Upvotes: 1

Views: 93

Answers (1)

宏杰李
宏杰李

Reputation: 12168

from lxml import etree

et = etree.fromstring(xml)
for array in et.xpath('//uima.cas.FSArray[@_id]'):
    print(array.xpath('@_id'), array.xpath('./i/text()'))

out:

['7429'] ['7409', '7419']
['7372'] ['7362', '7332', '7352', '7322', '7342']
['7285'] ['7245', '7275', '7255', '7265', '7235']

Upvotes: 1

Related Questions