2 回答
TA贡献1848条经验 获得超10个赞
XML 标签应该正确打开和关闭。从您发布的 XML 来看,似乎 XML 声明并非一开始。
<?xml version="1.0" encoding="utf-8"?>
这应该是一开始的。希望这可以帮助
TA贡献2065条经验 获得超13个赞
有问题的 XML 似乎是错误的,
这是正确版本的 XML 文件和 Go 代码
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Guru Jobs</title>
<link>http://www.guru.com</link>
<description>Guru Jobs</description>
<lastBuildDate>Sun, 15 Nov 2015 11:04:51 GMT</lastBuildDate>
<language>en-us</language>
<atom:link href='http://www.guru.com/rss/jobs/' rel="self" type="application/rss+xml" />
<item>
<title>Imaging for Bespoke Curtain Website</title>
<link>http://www.guru.com/jobs/imaging-for-bespoke-curtain-website/1203083</link>
<guid>http://www.guru.com/jobs/imaging-for-bespoke-curtain-website/1203083</guid>
<description><![CDATA[<b>Description:</b> Hi,We are currently developing a made to measure curtain website and are looking for help in develo...<br><b>Category:</b> Web, Software & IT<br><b>Required skills:</b> ecommerce, imaging software, opencart, web development<br><b>Fixed Price budget:</b> $500-$1k<br><b>Job type:</b> Public<br><b>Freelancer Location:</b> Worldwide<br>]]>
</description>
<pubDate>Mon, 04 Jan 2016 12:14:09 GMT</pubDate>
</item>
</channel>
</rss>
示例 Go 代码
package main
import (
"io/ioutil"
"encoding/xml"
"fmt"
)
type Rss2 struct {
ItemList []Item `xml:"channel>item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
GUID string `xml:"guid"`
}
func main() {
r := Rss2{}
xmlContent, _ := ioutil.ReadFile("example2.xml")
if err := xml.Unmarshal(xmlContent, &r); err != nil {
panic(err)
}
fmt.Println("RSS item :", r)
}
现在,您可以迭代并在 XML 中找到所需的数据。
- 2 回答
- 0 关注
- 171 浏览
添加回答
举报