GoLang parser XML, return null values

37 Views Asked by At

I want to parser a XML content with Golang using http request wih GET, but some values are null.

See this code:

package main

import (
    "encoding/xml"
    "fmt"
    "net/http"
    "time"

    "github.com/jvehent/service-go"
)

type Config struct {
    Topic            string
    Map              map[string]string
    Host             string
    Protocol         string
    Port             string
    Username         string
    Password         string
    Events           []string
    ReconnectTimeout int
}

type camera struct {
    config    Config
    client    *http.Client
    connected bool
    //eventChannel     chan event.Event
    reconnectTimeout time.Duration
    log              service.Logger
}

type XmlEvent struct {
    XMLName     xml.Name  `xml:"EventNotificationAlert"`
    IpAddress   string    `xml:"ipAddress"`
    Port        int       `xml:"portNo"`
    ChannelId   int       `xml:"channelID"`
    Time        time.Time `xml:"dateTime"`
    Id          int       `xml:"activePostCount"`
    Type        string    `xml:"eventType"`
    State       string    `xml:"eventState"`
    Description string    `xml:"eventDescription"`
    //Active      bool
    //Camera      *Config
}

func main() {

    x := []byte(`
<EventNotificationAlert version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<ipAddress>192.168.5.64</ipAddress>
<portNo>80</portNo>
<protocol>HTTP</protocol>
<macAddress>44:a6:42:ab:c5:c2</macAddress>
<channelID>1</channelID>
<dateTime>2024-02-26T20:40:55-3:00</dateTime>
<activePostCount>0</activePostCount>
<eventType>videoloss</eventType>
<eventState>inactive</eventState>
<eventDescription>videoloss alarm</eventDescription>
</EventNotificationAlert>
    `)

    // we initialize our Users array
    var xmlEvent XmlEvent
    // we unmarshal our byteArray which contains our
    // xmlFiles content into 'users' which we defined above
    xml.Unmarshal(x, &xmlEvent)

    //xmlEvent.Camera = &camera.config

    fmt.Printf("%s event: %s (%s - %d)", xmlEvent.IpAddress, xmlEvent.Type, xmlEvent.State, xmlEvent.Id)

    b, _ := xml.Marshal(xmlEvent)
    fmt.Println(string(b))

}

And with this code above, with simple XML structure, returns null, so ... Any clue?

Why xmlEvent.Type, xmlEvent.State and others are null values ?

Thanks in advance...

0

There are 0 best solutions below