登录
首页 >  Golang >  Go问答

使用 Go 解析 XML 文件有一个奇怪的行为

来源:stackoverflow

时间:2024-04-18 17:39:36 197浏览 收藏

大家好,今天本人给大家带来文章《使用 Go 解析 XML 文件有一个奇怪的行为》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容



  
    rd
    @@@
    xxx
    1000000
    2010-08-02t05:10:05+02:00
    q
    123456
    brute
  

  30000000000
  
    2010-08-02t00:00:00+02:00
    2010-08-02t23:59:59+02:00
    10
    kw
    cons
    ea
    
    
    
  
  
    2010-08-02t00:00:00+02:00
    2010-08-02t23:59:59+02:00
    10
    kvar
    cons
    eri
    
    
  

这是我用来解析它的结构。

type flow struct {
    xmlname    xml.name `xml:"courbe"`
    pathtofile string
    entete     flowheader
    corp       flowbody
}


type flowheader struct {
    xmlname         xml.name  `xml:"entete"`
    idflux          string    `xml:"identifiant_flux"`
    labelflux       string    `xml:"libelle_flux"`
    idemetteur      string    `xml:"identifiant_emetteur"`
    iddestinataire  uint32    `xml:"identifiant_destinataire"`
    datecreation    time.time `xml:"date_creation"`
    freqpublication string    `xml:"frequence_publication"`
    refdemande      uint32    `xml:"reference_demande"`
    naturecourbe    string    `xml:"nature_de_courbe_demandee"`
}

type flowbody struct {
    xmlname   xml.name `xml:"corps"`
    prm       string   `xml:"identifiant"`
    donneecdc flowdatacdc
}

type flowdatacdc struct {
    xmlname       xml.name                    `xml:"donnees_courbe"`
    horodatedebut time.time                   `xml:"horodatage_debut"`
    horodatefin   time.time                   `xml:"horodatage_fin"`
    granularite   uint32                      `xml:"granularite"`
    unite         string                      `xml:"unite_mesure"`
    grdmetier     string                      `xml:"grandeur_metier"`
    grdphysique   string                      `xml:"grandeur_physique"`
    donnes        []flowmeasurepoint `xml:"donnees_point_mesure"`
}

最初,我只有 1 个 donnees_courbe,所以还可以。现在,我有 2 个(只有第一个对我来说很重要,我想忽略第二个)

事情是,在 flowbody 结构中,我将最后一个字段更改为数组:

type flowBody struct {
    XMLName   xml.Name `xml:"Corps"`
    PRM       string   `xml:"Identifiant"`
    DonneeCDC []flowDataCDC
}

但它不起作用,我的数据为零。

如果我让它没有 donneecdc 数组,它会解析我的文件,但它说我的所有数据都有 unite_mesure=kvar,这显然不是我想要的。

我应该如何解析它?


解决方案


将结构标记添加到包含切片的字段应该可以工作:

type flowBody struct {
    XMLName   xml.Name      `xml:"Corps"`
    PRM       string        `xml:"Identifiant"`
    DonneeCDC []flowDataCDC `xml:"Donnees_Courbe"` // tag added
}

type flowDataCDC struct {
    XMLName       xml.Name           `xml:"Donnees_Courbe"` // this may be removed.
    HorodateDebut time.Time          `xml:"Horodatage_Debut"`
    HorodateFin   time.Time          `xml:"Horodatage_Fin"`
    Granularite   uint32             `xml:"Granularite"`
    Unite         string             `xml:"Unite_Mesure"`
    GrdMetier     string             `xml:"Grandeur_Metier"`
    GrdPhysique   string             `xml:"Grandeur_Physique"`
    Donnes        []flowMeasurePoint `xml:"Donnees_Point_Mesure"`
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>