1 回答
TA贡献1799条经验 获得超8个赞
我认为这应该对你有用。
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 关注
- 156 浏览
添加回答
举报