Home > OS >  Parsing XML processing instructions
Parsing XML processing instructions

Time:11-29

I've some processing instructions like the one below at the top of my XML file:

<?ID Object="AUTO_REPORT_OBJECT" Version="1.0"?>

I would like to read Object and Version attributes value using Go libraries. I'm using Go 1.19.

My XML file is like this:

<?xml version="1.0" encoding="UTF-8"?>
<?ID Object="AUTO_REPORT_OBJECT" Version="1.0"?>
<?xml-stylesheet type="text/xsl" href="../XML/ProdRep.xsl"?>
<!DOCTYPE Auto_Report SYSTEM "../XML/ProdRep.dtd" [
        <!ELEMENT Auto_Report (Production_Report )>
        ]>
<Auto_Report>
    <Production_Report Type="AUTO">
        ... more tags
    </Production_Report>
</Auto_Report>
<?End?>

CodePudding user response:

As far as XML is concerned, you can have anything you like in the content of a PI, so XML parsers aren't going to help with this - you have to parse the content by hand.

One option would be to take the data part of the PI, put "<e " at the start and "/>" at the end, and then put it through an XML parser.

CodePudding user response:

I don't know Go, but you can parse this with python 3.11.

import xml.etree.ElementTree as ET

for event, elem in ET.iterparse('your.xml', events=("pi")):
    if event == "pi":
        print(ET.tostring(elem))
  • Related