I'm trying to unmarshal an XML file, but it's not finding my root node snapshot
, which is keeping me from being able to unmarshal the rest of my file.
I'm calling parser.go
from main.go
, as well as the Validate
function.
parser.go
type Parser struct {
file *ConfigXML
filepath string
}
//Load the file
func (parser *Parser) Load() error {
b, err := helpers.LoadFile(parser.filepath)
if err != nil {
return err
}
parser.file = nil
xml.Unmarshal(b, &parser.file)
return nil
}
func (parser *Parser) Validate() error {
if len(parser.file.snapshot) == 0 {
return fmt.Errorf("node snapshot does not exist")
}
return nil
}
While my XML file is large, I'll post a snippet:
<?xml version='1.0' encoding='UTF-8'?>
<snapshot>
<ENBEquipment id="233443234543" model="XYZ" version="LR_16_02_L">
<attributes>
<administrativeState>unlocked</administrativeState>
</attributes>
</ENBEquipment>
</snapshot>
The structs I've created to unmarshal to:
type ConfigXML struct {
snapshot []Snapshot
FileName string
}
// Snapshot is root <snapshot>
type Snapshot struct {
ENBEquipment []ENBEquipment
}
// ENBEquipment subtag of <snapshot> -> <ENBEquipment>
type ENBEquipment struct {
ID string `xml:"id,attr"`
DeviceName string `xml:"id,attr"`
SoftwareVersion string `xml:"version,attr"`
ElementType string `xml:"model,attr"`
Enb []Enb
}
However when I attempt to run with go run main.go
, I get:
ERRO[2018-06-12 09:45:34] parsing failed error="node snapshot does not exist" filename=/tmp/datain/xyz/123/agg/233443234543.xml
If I take out the Validation of snapshot
, the file unmarshals fine. Why is this not finding the node?