去解析XML文件

时间:2017-11-04 14:40:16

标签: go xml-parsing

我想解析一个xml文件并打印值。

<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>

在这个例子中我只有一个警报,但我可以有更多。我写了2个包

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"`
}

我的第二个包

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\n\n", 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\n", al.Alert[i].StartedAt)
    }
}

我可以打印Time,Type,Identifier和DeclaredBy但变量StartedAt是空的。有一些我不明白的东西。我认为我的结构没有正确定义。 如果有人可以帮助我......谢谢!

1 个答案:

答案 0 :(得分:0)

看起来您需要将StartedAt定义为不仅仅是一个字符串。 Take a look at this question and example。您需要将StartedAt定义为它自己的结构,以便您可以访问它的innerxml字符串。

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"`
}