英文:
Go xml unmarshal doesn't work unless I remove preceding xml version="1.0" encoding="ISO-8859-1"
问题
我已经花了几个小时来弄清楚这个问题,但我不明白为什么它只返回一个空字符串。如果我将响应体剪切并粘贴到一个变量中,并删除 "?xml version="1.0" encoding="ISO-8859-1" ?",它就可以正常工作。
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)
}
}
有人能看出我犯了什么错误吗?感谢任何帮助。
英文:
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.
答案1
得分: 1
根据codefreak提供的链接,答案是由moraes在“Updated answer for 2015 & beyond”中给出的。
更新的工作代码:
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)
}
}
谢谢!
英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论