<?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
,这显然不是我想要的。
我应该如何解析呢?
答案 0 :(得分:0)
将struct标签添加到包含切片的字段中应该可以:
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"`
}