0

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?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • 1
    That's a processing instruction and here's how to get'em https://stackoverflow.com/a/49131506/2834978 or https://stackoverflow.com/questions/30360183/finding-and-converting-xml-processing-instructions-using-python – LMC May 05 '23 at 15:35

2 Answers2

1
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
Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
1

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")
Hermann12
  • 1,709
  • 2
  • 5
  • 14