去解析XML文件

i would like to parse a xml file and print values.

<alerts time="2017-09-14T15:46:00+02:00">
  <alert type="A" identifier="123" declaredBy="Bob" />
    <startedAt>20171010212210</startedAt>
    <lastUpdate bySource="X">
      <updatedAt>20171010213655</updatedAt>
      <eventConfirmation>unconfirmed</eventConfirmation>
      <additional>
        <data name="numberOfOccurences">1</data>
      </additional>
    </lastUpdate>
    <labels>
      <label language="FR">attaque DNS</Label>
    </labels>
  </alert>
</alerts>

in this example i have just one alert but i can have more. I wrote 2 packages

package alerts

import "encoding/xml"

// Alert structure
type Alerts struct {
    XMLName  xml.Name `xml:"alerts"`
    Time     string   `xml:"time,attr"`
    Alert    []alert  `xml:"alert"`
}

type lastUpdate struct {
    XMLName           xml.Name   `xml:"lastUpdate"`
    BySource          string     `xml:"bySource,attr"`
    UpdatedAt         string     `xml:"updatedAt"`
    EventConfirmation string     `xml:"eventConfirmation"`
    Additional        additional `xml:"additional"`
}

type additional struct {
    XMLName xml.Name `xml:"additional"`
    Data    data     `xml:"data"`
}

type data struct {
    XMLName            xml.Name `xml:"data"`
    NumberOfOccurences int      `xml:"numberOfOccurences,attr"`
}

type labels struct {
    XMLName xml.Name `xml:"labels"`
    Label   []label  `xml:"label"`
}

type label struct {
    XMLName       xml.Name `xml:"label"`
    Label         string   `xml:"label"`
    LabelLanguage string   `xml:"language,attr"`
}

type alert struct {
    XMLName xml.Name `xml:"alert"`
    Type             string     `xml:"type,attr"`
    Identifier       string     `xml:"identifier,attr"`
    DeclaredBy       string     `xml:"declaredBy,attr"`
    StartedAt        string     `xml:"startedAt"`
    Lastupdate       lastUpdate `xml:"lastupdate"`
    Labels           labels     `xml:"label"`
}

my second package

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"

    "github.com/user/xmlparser/alert"
    "github.com/golang/glog"
)

func main() {

    //open XML file given in argument
    xmlfile, err := os.Open(os.Args[1])
    if err != nil {
        glog.Fatalf("opening file %s - error : %s", os.Args[1], err)
    }

    defer xmlfile.Close()

    // read our opened xmlFile as a byte array.
    byteValue, _ := ioutil.ReadAll(xmlfile)

    // we initialize our alerts array
    var al alerts.Alerts

    // we unmarshal our byteArray which contains our
    // xmlFiles content into 'al' which we defined above
    xml.Unmarshal(byteValue, &al)

    for i := 0; i < len(al.Alert); i++ {
        fmt.Printf("Alert time : %s

", al.Time)

        fmt.Println("Type: " + al.Alert[i].Type)
        fmt.Println("Identifier: " + al.Alert[i].Identifier)
        fmt.Println("declaredBy: " + al.Alert[i].DeclaredBy)

        fmt.Printf("startAt: %s
", al.Alert[i].StartedAt)
    }
}

I can print Time, Type, Identifier and DeclaredBy but the variable StartedAt is empty. There is something that i don't understand. I think that my structure is not correctly defined. If someone can help me ... Thanks !!!

It looks like you need to define StartedAt as more than just a string. Take a look at this question and example. You need to define StartedAt as it's own struct so you can access it's innerxml string.

type StartedAt struct {
   Raw  string `xml:",innerxml"`
}

type alert struct {
   XMLName xml.Name `xml:"alert"`
   Type             string     `xml:"type,attr"`
   Identifier       string     `xml:"identifier,attr"`
   DeclaredBy       string     `xml:"declaredBy,attr"`
   StartedAt        StartedAt  `xml:"startedAt"`
}