XML解析返回带换行符的字符串

时间:2019-02-02 05:35:01

标签: xml go

我试图通过站点地图解析XML,然后遍历该地址以获取Go中帖子的详细信息。但是我收到了这个奇怪的错误:

  

:URL中的第一个路径段不能包含冒号

这是代码段:

type SitemapIndex struct {
    Locations []Location `xml:"sitemap"`
}

type Location struct {
    Loc string `xml:"loc"`
}

func (l Location) String() string {
    return fmt.Sprintf(l.Loc)
}

func main() {
    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
    bytes, _ := ioutil.ReadAll(resp.Body)
    var s SitemapIndex
    xml.Unmarshal(bytes, &s)
    for _, Location := range s.Locations {
        fmt.Printf("Location: %s", Location.Loc)
        resp, err := http.Get(Location.Loc)
        fmt.Println("resp", resp)
        fmt.Println("err", err)
    }
}

输出:

Location: 
https://www.washingtonpost.com/news-sitemaps/politics.xml
resp <nil>
err parse 
https://www.washingtonpost.com/news-sitemaps/politics.xml
: first path segment in URL cannot contain colon
Location: 
https://www.washingtonpost.com/news-sitemaps/opinions.xml
resp <nil>
err parse 
https://www.washingtonpost.com/news-sitemaps/opinions.xml
: first path segment in URL cannot contain colon
...
...

我的猜测是Location.Loc在实际地址之前和之后返回新行。 例如:\nLocation: https://www.washingtonpost.com/news-sitemaps/politics.xml\n

因为对URL进行硬编码可以按预期工作:

for _, Location := range s.Locations {
        fmt.Printf("Location: %s", Location.Loc)
        test := "https://www.washingtonpost.com/news-sitemaps/politics.xml"
        resp, err := http.Get(test)
        fmt.Println("resp", resp)
        fmt.Println("err", err)
    }

输出,如您所见,错误为nil:

Location: 
https://www.washingtonpost.com/news-sitemaps/politics.xml
resp &{200 OK 200 HTTP/2.0 2 0 map[Server:[nginx] Arc-Service:[api] Arc-Org-Name:[washpost] Expires:[Sat, 02 Feb 2019 05:32:38 GMT] Content-Security-Policy:[upgrade-insecure-requests] Arc-Deployment:[washpost] Arc-Organization:[washpost] Cache-Control:[private, max-age=60] Arc-Context:[index] Arc-Application:[Feeds] Vary:[Accept-Encoding] Content-Type:[text/xml; charset=utf-8] Arc-Servername:[api.washpost.arcpublishing.com] Arc-Environment:[index] Arc-Org-Env:[washpost] Arc-Route:[/feeds] Date:[Sat, 02 Feb 2019 05:31:38 GMT]] 0xc000112870 -1 [] false true map[] 0xc00017c200 0xc0000ca370}
err <nil>
Location: 
...
...

但是我对Go语言很陌生,所以我不知道出了什么问题。你能告诉我我哪里错了吗?

2 个答案:

答案 0 :(得分:1)

您确实是对的,问题出在换行符上。如您所见,您正在使用Printf而不添加任何\n,并且在输出的开头添加了一个,在输出的末尾添加了一个。

您可以使用strings.Trim删除这些换行符。 an example在这里处理您要解析的站点地图。修剪字符串后,您将可以在其上调用http.Get,而不会发生任何错误。

func main() {
    var s SitemapIndex
    xml.Unmarshal(bytes, &s)

    for _, Location := range s.Locations {
        loc := strings.Trim(Location.Loc, "\n")
        fmt.Printf("Location: %s\n", loc)
    }
}

此代码正确输出了不含换行符的位置,如预期的那样:

Location: https://www.washingtonpost.com/news-sitemaps/politics.xml
Location: https://www.washingtonpost.com/news-sitemaps/opinions.xml
Location: https://www.washingtonpost.com/news-sitemaps/local.xml
Location: https://www.washingtonpost.com/news-sitemaps/sports.xml
Location: https://www.washingtonpost.com/news-sitemaps/national.xml
Location: https://www.washingtonpost.com/news-sitemaps/world.xml
Location: https://www.washingtonpost.com/news-sitemaps/business.xml
Location: https://www.washingtonpost.com/news-sitemaps/technology.xml
Location: https://www.washingtonpost.com/news-sitemaps/lifestyle.xml
Location: https://www.washingtonpost.com/news-sitemaps/entertainment.xml
Location: https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml

之所以在Location.Loc字段中包含这些换行符,是因为此URL返回了XML。条目以下这种形式:

<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml
</loc>
</sitemap>

如您所见,loc元素内的内容前后都有换行符。

答案 1 :(得分:1)

请参阅修改后的代码中嵌入的注释,以描述和解决问题

func main() {
    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
    bytes, _ := ioutil.ReadAll(resp.Body)
    var s SitemapIndex
    xml.Unmarshal(bytes, &s)
    for _, Location := range s.Locations {
            // Note that %v shows that there are indeed newlines at beginning and end of Location.Loc
            fmt.Printf("Location: (%v)", Location.Loc)
            // solution: use strings.TrimSpace to remove newlines from Location.Loc
            resp, err := http.Get(strings.TrimSpace(Location.Loc))
            fmt.Println("resp", resp)
            fmt.Println("err", err)
    }

}