I have an XML file
<?xml version="1.0" encoding="UTF-8"?>
<?foo class="abc" options="bar,baz"?>
<document>
...
</document>
and I'm interested in the processing instruction foo
and its attributes.
I can use ET.iterparse
for reading the PI, but it escapes me how to access the attributes as a dictionary – .attrib
only gives an empty dict.
import xml.etree.ElementTree as ET
for _, elem in ET.iterparse("data.xml", events=("pi",)):
print(repr(elem.tag))
print(repr(elem.text))
print(elem.attrib)
<function ProcessingInstruction at 0x7f848f2f7ba0>
'foo class="abc" options="bar,baz"'
{}
Any hints?