diff --git a/feed_test.go b/feed_test.go index dca55b3..f90d5ee 100644 --- a/feed_test.go +++ b/feed_test.go @@ -76,6 +76,32 @@ func Test_RssAuthor(t *testing.T) { } } +func Test_Extensions(t *testing.T) { + content, _ := ioutil.ReadFile("testdata/extension.rss") + feed := New(1, true, chanHandler, itemHandler) + feed.FetchBytes("http://example.com", content, nil) + + extension := feed.Channels[0].Items[0].Extensions["http://www.sec.gov/Archives/edgar"] + + companyExpected := "Cellular Biomedicine Group, Inc." + companyName := *extension.Childrens[0] + if companyName.Value != companyExpected { + t.Errorf("Expected company to be %s but found %s", companyExpected, companyName.Value) + } + + files := *extension.Childrens[11] + fileSizeExpected := 10 + if len(files.Childrens) != 10 { + t.Errorf("Expected files size to be %s but found %s", fileSizeExpected, len(files.Childrens)) + } + + file := *files.Childrens[0] + fileExpected := "cbmg_10qa.htm" + if file.Attrs["file"] != fileExpected { + t.Errorf("Expected file to be %s but found %s", fileExpected, len(file.Attrs["file"])) + } +} + func Test_CData(t *testing.T) { content, _ := ioutil.ReadFile("testdata/iosBoardGameGeek.rss") feed := New(1, true, chanHandler, itemHandler) diff --git a/item.go b/item.go index ea13f85..1a301e8 100644 --- a/item.go +++ b/item.go @@ -23,6 +23,8 @@ type Item struct { Generator *Generator Contributors []string Content *Content + + Extensions map[string]*Extension } func (i *Item) Key() string { diff --git a/rss.go b/rss.go index b6c6d73..6855b3f 100644 --- a/rss.go +++ b/rss.go @@ -6,6 +6,13 @@ import ( xmlx "github.com/jteeuwen/go-pkg-xmlx" ) +type Extension struct { + Name string + Value string + Attrs map[string]string + Childrens []*Extension +} + var days = map[string]int{ "Monday": 1, "Tuesday": 2, @@ -174,9 +181,39 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) { } } + tl = item.SelectNodes("*", "*") + i.Extensions = make(map[string]*Extension) + for _, lv := range tl { + e, ok := getExtension(lv) + if ok { + i.Extensions[lv.Name.Space] = e + } + } + ch.Items = append(ch.Items, i) } } this.Channels = foundChannels return } + +func getExtension(node *xmlx.Node) (*Extension, bool) { + if node.Name.Space != "" { + var extension Extension + extension = Extension{Name: node.Name.Local, Value: node.GetValue()} + extension.Attrs = make(map[string]string) + for _, x := range node.Attributes { + extension.Attrs[x.Name.Local] = x.Value + } + + for _, y := range node.Children { + if c, ok := getExtension(y); ok { + extension.Childrens = append(extension.Childrens, c) + } + } + + return &extension, true + } else { + return nil, false + } +} diff --git a/testdata/extension.rss b/testdata/extension.rss new file mode 100644 index 0000000..8619389 --- /dev/null +++ b/testdata/extension.rss @@ -0,0 +1,48 @@ + + + + Extensions Test + http://test.extensions.net + Just a Test + en-US + Wed, 27 Mar 2013 12:30:18 PDT + + http://test.extensions.net + http://test.extensions.net/test.jpg + Extensions Test + + + Cellular Biomedicine Group, Inc. (0001378624) (Filer) + http://www.sec.gov/Archives/edgar/data/1378624/000135448813006749/0001354488-13-006749-index.htm + http://www.sec.gov/Archives/edgar/data/1378624/000135448813006749/0001354488-13-006749-xbrl.zip + + 10-Q/A + Fri, 06 Dec 2013 17:29:22 EST + + Cellular Biomedicine Group, Inc. + 10-Q/A + 12/06/2013 + 0001378624 + 0001354488-13-006749 + 000-52282 + 20131206172922 + 20130630 + 7 + 6199 + 1231 + + + + + + + + + + + + + + + +