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 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题