I have a basic xml field which looks like this <cpe test="123">cpe:/a:openbsd:openssh:5.3p1</cpe> and I want to unmarshal it using Go. I create the object and while it normally unmarshals and prints the test attribute, the content inside the brackets is always printed as empty.
package main
import (
"fmt"
"encoding/xml"
)
type Cpe struct {
XMLName xml.Name `xml:"cpe"`
Value string `xml:"chardata"`
Res string `xml:"test,attr"`
}
func main() {
var inputXML = `<cpe test="123">cpe:/a:openbsd:openssh:5.3p1</cpe>`
byteValue := []byte(inputXML)
// Create the onject
var cpe Cpe
// Unmarshal the xml using the Address object
xml.Unmarshal(byteValue, &cpe)
// prints the Res and Address Value
fmt.Println(cpe.Res)
fmt.Println(cpe.Value)
}
The output of those things are 123 and nothing. By googling I find out that I can use innerXML instead of chardata, which had the same result.
Playground: https://play.golang.org/p/7eyoXjnOlS4