I have an XML file
<?xml version="1.0" encoding="UTF-8"?>
<?foo class="abc"?>
<document>
...
</document>
frpm which I'd like to read the foo prolog using Python.
import xml.etree.ElementTree as ET
tree = ET.parse("bar.xml")
# ??
Any hints?
I have an XML file
<?xml version="1.0" encoding="UTF-8"?>
<?foo class="abc"?>
<document>
...
</document>
frpm which I'd like to read the foo prolog using Python.
import xml.etree.ElementTree as ET
tree = ET.parse("bar.xml")
# ??
Any hints?
from xml.dom.minidom import parse
tree = parse('bar.xml')
node = tree.childNodes[0]
print(f'{node.nodeName}, {node.nodeValue}')
# or
for node in tree.childNodes:
if node.nodeName == 'foo':
print(node.data)
break
Iterparse() can show the event pi content:
import xml.etree.ElementTree as ET
for event, elem in ET.iterparse("bar.xml", events=("start", "end", "pi")):
print(event, elem.text)
or with lxml:
from lxml import etree
tree = etree.parse("bar.xml")
result = tree.xpath('//processing-instruction()')
print(result)
or with SAX:
from xml.sax import make_parser, handler
class ProInst(handler.ContentHandler):
def processingInstruction(self, target, data):
print ('processing instruction:', target, data)
parser = make_parser()
b = ProInst()
parser.setContentHandler(b)
parser.parse("bar.xml")