0

I am trying to parse an XML file (source: https://www.prix-carburants.gouv.fr/rubrique/opendata/) in Golang to transform it into a tidy csv. Currently, I am stuck.

The structures seem to be well defined, I can read the correct number of structures, but all the values are empty after reading the file.

Example xml:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<pdv_liste>
  <pdv id="59780003" latitude="5059477.455" longitude="325781.84717474" cp="59780" pop="R">
    <adresse>RD 93 GRANDE RUE</adresse>
    <ville>Camphin-en-P�v�le</ville>
    <prix nom="Gazole" id="1" maj="2021-11-09 11:01:44" valeur="1.548"/>
    <prix nom="E85" id="3" maj="2021-11-08 14:11:47" valeur="0.687"/>
    <prix nom="E10" id="5" maj="2021-11-09 11:01:45" valeur="1.634"/>
    <prix nom="SP98" id="6" maj="2021-11-09 11:01:45" valeur="1.720"/>
  </pdv>
</pdv_liste>

Here is my code:

package main

import (
    "encoding/xml"
    "fmt"
    "io"
    "os"

    "golang.org/x/text/encoding/charmap"
)

type Prix struct {
    XMLName xml.Name `xml:"prix"`
    id      string   `xml:"id,attr"`
    nom     string   `xml:"nom,attr"`
    maj     string   `xml:"maj,attr"`
    valeur  string   `xml:"valeur,attr"`
}

type Adresse struct {
    XMLName xml.Name `xml:"adresse"`
    Value   string   `xml:",chardata"`
}

type Pdv struct {
    XMLName   xml.Name `xml:"pdv"`
    id        string   `xml:"id,attr"`
    latitude  string   `xml:"latitude,attr"`
    longitude string   `xml:"longitude,attr"`
    cp        string   `xml:"cp,attr"`
    pop       string   `xml:"pop,attr"`
    adresse   Adresse  `xml:"adresse"`
    ville     string   `xml:"ville,chardata"`
    Prix      []Prix   `xml:"prix"`
}

type Pdv_liste struct {
    XMLName xml.Name `xml:"pdv_liste"`
    PDVs    []Pdv    `xml:"pdv"`
}

func makeCharsetReader(charset string, input io.Reader) (io.Reader, error) {
    if charset == "ISO-8859-1" {
        // Windows-1252 is a superset of ISO-8859-1, so should do here
        return charmap.Windows1252.NewDecoder().Reader(input), nil
    }
    return nil, fmt.Errorf("Unknown charset: %s", charset)
}

func main() {

    xmlFile, err := os.Open("prix.xml")
    if err != nil {
        fmt.Println(err)
    }

    defer xmlFile.Close()

    var pdv_liste Pdv_liste

    decoder := xml.NewDecoder(xmlFile)
    decoder.CharsetReader = makeCharsetReader
    err = decoder.Decode(&pdv_liste)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Nombre de pdvs:", len(pdv_liste.PDVs))

    fmt.Println(pdv_liste.PDVs[0])
    fmt.Println("PDV.cp: ", pdv_liste.PDVs[0].cp)
}

Any ideas on what I am doing wrong?

catkfr
  • 1
  • 1
    You must export struct fields, start them with an uppercase letter (e.g. `latitude` => `Latitude`). – icza Nov 14 '21 at 13:39
  • Thanks @icza, I just saw your answer here (https://stackoverflow.com/a/32674930/17409497) which works... I had searched stackoverflow before posting, but only found your answer to this other post once I had posted. – catkfr Nov 14 '21 at 13:46

0 Answers0