1 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
我認(rèn)為這應(yīng)該對(duì)你有用。
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Namespace string `xml:"xmlns,attr"`
Schema string `xml:"xmlns:xsi,attr"`
SchemaLocation string `xml:"xsi:schemaLocation,attr"`
Root *URLItem
}
type URLItem struct {
XMLName xml.Name `xml:"url"`
Loc string `xml:"loc"`
LastMod string `xml:"lastmod,omitempty"`
Urls []*URLItem
}
func (s *Sitemap) AddURL(key string, url string) {
node, found := findURLItemRecursive(s.Root, key)
if found {
node.Urls = append(node.Urls, &URLItem{Loc: url})
}
}
func findURLItemRecursive(urlItem *URLItem, key string) (*URLItem, bool) {
if urlItem.Loc == key {
return urlItem, true
}
for _, urlItem := range urlItem.Urls {
item, found := findURLItemRecursive(urlItem, key)
if found {
return item, found
}
}
return nil, false
}
- 1 回答
- 0 關(guān)注
- 188 瀏覽
添加回答
舉報(bào)