vinniyo
vinniyo

Reputation: 877

Go xml unmarshal doesn't work unless I remove preceding xml version="1.0" encoding="ISO-8859-1"

I've spent a few hours trying to figure this out but I cannot see why this wont return anything but an empty string. If I cut and paste the response body to a variable and remove the ?xml version="1.0" encoding="ISO-8859-1" ? it works fine.

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "net/http"
)

type entry struct {
    XMLName  xml.Name `xml:"entry"`
    Title    string   `xml:"title"`
    Link     string   `xml:"link"`
    Summary  string   `xml:"summary"`
    Updated  string   `xml:"updated"`
    Catagory string   `xml:"catagory"`
    ID       string   `xml:"id"`
}

type Feed struct {
    XMLName xml.Name `xml:"feed"`
    Title   string   `xml:"title"`
    Entry   []entry  `xml:"entry"`
}

func main() {
    resp, err := http.Get("https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&type=4&company=&dateb=&owner=include&start=0&count=2&output=atom")
    if err != nil {
        fmt.Println("Get sec main xml error: %s", err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    //fmt.Println(string(body))
    var feedData Feed
    xml.Unmarshal(body, &feedData)

    fmt.Println(feedData.Title)

    for _, entry := range feedData.Entry {
        fmt.Println(entry.ID)
    }
}

Can anyone see where I'm making the mistake? Thanks for any assistance.

Upvotes: 0

Views: 1289

Answers (1)

vinniyo
vinniyo

Reputation: 877

from the link provided by codefreak the answer was by moraes in "Updated answer for 2015 & beyond" Updated working code:

package main

import (
    "encoding/xml"
    "fmt"
    "golang.org/x/net/html/charset"
    "net/http"
)

type entry struct {
    XMLName  xml.Name `xml:"entry"`
    Title    string   `xml:"title"`
    Link     string   `xml:"link"`
    Summary  string   `xml:"summary"`
    Updated  string   `xml:"updated"`
    Catagory string   `xml:"catagory"`
    ID       string   `xml:"id"`
}

type Feed struct {
    XMLName xml.Name `xml:"feed"`
    Title   string   `xml:"title"`
    Entry   []entry  `xml:"entry"`
}

func main() {
    resp, err := http.Get("https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&type=4&company=&dateb=&owner=include&start=0&count=2&output=atom")
    if err != nil {
        fmt.Println("Get sec main xml error: %s", err)
    }
    defer resp.Body.Close()
    var feedData Feed
    decoder := xml.NewDecoder(resp.Body)
    decoder.CharsetReader = charset.NewReaderLabel
    err = decoder.Decode(&feedData)

    fmt.Println(feedData.Title)

    for _, entry := range feedData.Entry {
        fmt.Println(entry.ID)
    }
}

Thank you!

Upvotes: 1

Related Questions