douzhi3586 2019-07-17 07:20
浏览 36
已采纳

用Go解析XML文件的行为很奇怪

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

Here are the structures I use to parse it.

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

Initially, I had only 1 Donnees_Courbe, so it was OK. Now, I have 2 ( Only the first one is important to me, I want to ignore the second one )

Thing is, in the flowBody struct, I change the last field to an array:

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

but it doesnt work, my data is nil.

If I let it without an DonneeCDC array, it will parse my file, but it says that all my data has Unite_Mesure=kVAr, which is obviously not what I want.

How should I parse it well ?

  • 写回答

1条回答 默认 最新

  • dqdes60666 2019-07-17 10:05
    关注

    Adding the struct tag to the field containing the slice should work:

    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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持