Unmarshalling a processing instruction in Go using encoding/xml

Viewed 50

I have an XML file (MusicXML produced by Guitar Pro 7.0) and it contains embedded processing instructions beyond the usual <?xml version="1.0" encoding="UTF-8" ?> scattered throughout the file. I am trying to parse this into a hierarchy of Go struct types using xml.Unmarshal(...) with tags on my struct fields.

The MusicXML has processing instructions of the form <?GP ... ?> which I would like to retain so that when I write the XML back out, it will be accepted by Guitar Pro.

I cannot figure out how to unmarshall the processing instructions.

I have tried including a field with type xml.ProcInst and even tried tagging it as xml:",innerxml", but that did not work.

I briefly looked through the unmarshall source code in the Go encoding/xml library, but did not see any special tags values that might help (and the documentation doesn't have anything either).

Clarification: Please note that the element that contains the processing instruction sometimes also contains other elements that must be parsed:

<staff-details number="1">
    <?GP 
    <root>
    <staff-tuning line="1">
    <tuning-step>G</tuning-step>
    <tuning-octave>3</tuning-octave>
    </staff-tuning>
    <staff-tuning line="2">
    <tuning-step>C</tuning-step>
    <tuning-octave>4</tuning-octave>
    </staff-tuning>
    <staff-tuning line="3">
    <tuning-step>E</tuning-step>
    <tuning-octave>4</tuning-octave>
    </staff-tuning>
    <staff-tuning line="4">
    <tuning-step>A</tuning-step>
    <tuning-octave>4</tuning-octave>
    </staff-tuning>
    </root>
    ?>
</staff-details>

<staff-details number="2">
 <staff-lines>4</staff-lines>
 <staff-tuning line="1">
  <tuning-step>G</tuning-step>
  <tuning-octave>3</tuning-octave>
 </staff-tuning>
 <staff-tuning line="2">
  <tuning-step>C</tuning-step>
  <tuning-octave>4</tuning-octave>
 </staff-tuning>
 <staff-tuning line="3">
  <tuning-step>E</tuning-step>
  <tuning-octave>4</tuning-octave>
 </staff-tuning>
 <staff-tuning line="4">
  <tuning-step>A</tuning-step>
  <tuning-octave>4</tuning-octave>
 </staff-tuning>
</staff-details>
0 Answers
Related