From https://golang.org/pkg/encoding/xml/#Unmarshal:
If the struct has a field of type []byte
or string
with tag
",innerxml"
, Unmarshal accumulates the raw XML nested inside the
element in that field.
You can just use the struct tag ",innerxml"
to a field of type string
or []byte
that's inside the element you're trying to extract XML from. You'll need to use a sub-struct. Also note that the selection query of the XML library starts at the first element (It's quite strange). So you can't start the tag with nexgen_audio_export>
, but rather go straight to audio>
.
Here's working example code:
package main
import (
"encoding/xml"
"fmt"
)
// Encoding had to be changed to UTF-8
var input = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<nexgen_audio_export>
<audio ID="id_1667331726_30393658">
<type>Song</type>
<status>Playing</status>
<played_time>09:41:18</played_time>
<composer>Frederic Delius</composer>
<title>Violin Sonata No.1</title>
<artist>Tasmin Little, violin; Piers Lane, piano</artist>
<comments>
<p>Comment line1</p>
<p>Comment <b>line2</b></p>
<p>Comment line3</p>
</comments>
</audio>
</nexgen_audio_export>`)
type audio struct {
Comments struct {
InnerXML string `xml:",innerxml"`
} `xml:"audio>comments"`
}
func main() {
var a audio
err := xml.Unmarshal(input, &a)
if err != nil {
panic(err)
}
fmt.Println(a.Comments.InnerXML)
}
Playground link: https://play.golang.org/p/LAL2V0zExc