python - Problems parsing an XML file with xml.etree.ElementTree -
i have parse xml files contain entries like
<error code="unknowndevice"> <description /> </error>
which defined elsewhere as
<group name="error definitions"> <errordef id="0x11" name="unknowndevice"> <description>indicated device unknown</description> </errordef> ... </group>
given
import xml.etree.elementtree et parser = et.xmlparser() parser.parser.useforeigndtd(true) tree = et.parse(inputfilename, parser=parser) root = tree.getroot()
how can values errordef
? mean value of id
, of description
?
how can search & extract values, using unknowndevice
?
[update] error groups have differing names, of format "xxx error definitions", "yyy error definitions", etc
further, seem nested @ different depths in different documents.
given error's title, e.g "unknowndevice", how can search under root corresponding id
, description values?
can go directly them, using e.g "unknowndevice", or have search first error groups?
if have this:
<group name="error definitions"> <errordef id="0x11" name="unknowndevice"> <description>indicated device unknown</description> </errordef> ... </group>
and want value of description
, id
every errordef
element, this:
for err in tree.xpath('//errordef'): print err.get('id'), err.find('description').text
which give like:
0x11 indicated device unknown
Comments
Post a Comment