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

huangapple go评论80阅读模式
英文:

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!

huangapple
  • 本文由 发表于 2015年11月19日 14:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/33797182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定