<?xml version="1.0" encoding="UTF-8"?>
<Courbe>
<Entete>
<Identifiant_Flux>RD</Identifiant_Flux>
<Libelle_Flux>@@@</Libelle_Flux>
<Identifiant_Emetteur>xxx</Identifiant_Emetteur>
<Identifiant_Destinataire>1000000</Identifiant_Destinataire>
<Date_Creation>2010-08-02T05:10:05+02:00</Date_Creation>
<Frequence_Publication>Q</Frequence_Publication>
<Reference_Demande>123456</Reference_Demande>
<Nature_De_Courbe_Demandee>Brute</Nature_De_Courbe_Demandee>
</Entete>
<Corps>
<Identifiant>30000000000</Identifiant>
<Donnees_Courbe>
<Horodatage_Debut>2010-08-02T00:00:00+02:00</Horodatage_Debut>
<Horodatage_Fin>2010-08-02T23:59:59+02:00</Horodatage_Fin>
<Granularite>10</Granularite>
<Unite_Mesure>kW</Unite_Mesure>
<Grandeur_Metier>CONS</Grandeur_Metier>
<Grandeur_Physique>EA</Grandeur_Physique>
<Donnees_Point_Mesure Horodatage ="2010-08-02T00:00:00+02:00" Valeur_Point ="10" Statut_Point ="R"></Donnees_Point_Mesure>
<Donnees_Point_Mesure Horodatage ="2010-08-02T00:10:00+02:00" Valeur_Point ="10" Statut_Point ="R"></Donnees_Point_Mesure>
<Donnees_Point_Mesure Horodatage ="2010-08-02T00:20:00+02:00" Valeur_Point ="10" Statut_Point ="R"></Donnees_Point_Mesure>
</Donnees_Courbe>
<Donnees_Courbe>
<Horodatage_Debut>2010-08-02T00:00:00+02:00</Horodatage_Debut>
<Horodatage_Fin>2010-08-02T23:59:59+02:00</Horodatage_Fin>
<Granularite>10</Granularite>
<Unite_Mesure>kVAr</Unite_Mesure>
<Grandeur_Metier>CONS</Grandeur_Metier>
<Grandeur_Physique>ERI</Grandeur_Physique>
<Donnees_Point_Mesure Horodatage ="2010-08-02T00:00:00+02:00" Valeur_Point ="6" Statut_Point ="R"></Donnees_Point_Mesure>
<Donnees_Point_Mesure Horodatage ="2010-08-02T00:10:00+02:00" Valeur_Point ="5" Statut_Point ="R"></Donnees_Point_Mesure>
</Donnees_Courbe>
</Corps>
</Courbe>
这是我用来解析它的结构。
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
,这显然不是我想要的想要。
我该如何解析好?
最佳答案
将结构标记添加到包含 slice 的字段应该有效:
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"`
}
关于xml - 使用 Go 解析 XML 文件有一个奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57070542/