// ForEach returns Count and MaxRows and any error that 'each' wont handle func (c *StandardXMLSearchResult) ForEach(match minidom.Matcher, each minidom.EachDOM) (int, bool, error) { defer c.body.Close() count := 0 maxrows := false md := minidom.MiniDom{ StartFunc: func(start xml.StartElement) { switch start.Name.Local { case "COUNT": count, _ = countTag(start).Parse() case "MAXROWS": maxrows = true } }, // quit on the the xml tag EndFunc: func(end xml.EndElement) bool { switch end.Name.Local { case XMLElemRETS, XMLElemRETSStatus: return true } return false }, } err := md.Walk(c.parser, match, each) return count, maxrows, err }
func TestSimple(t *testing.T) { doms := ioutil.NopCloser(strings.NewReader(example)) parser := xml.NewDecoder(doms) listings := Listings{} // minidom isnt necessary but its crazy useful for massive streams md := minidom.MiniDom{ StartFunc: func(start xml.StartElement) { switch start.Name.Local { case "Listings": attrs := map[string]string{} for _, v := range start.Attr { attrs[v.Name.Local] = v.Value } listings.ListingsKey = attrs["listingsKey"] listings.Version = attrs["version"] listings.VersionTimestamp = attrs["versionTimestamp"] listings.Language = attrs["lang"] case "Disclaimer": parser.DecodeElement(listings.Disclaimer, &start) } }, // quit on the the xml tag EndFunc: minidom.QuitAt("Listings"), } err := md.Walk(parser, minidom.ByName("Listing"), ToListing(func(l Listing, err error) error { listings.Listings = append(listings.Listings, l) return err })) testutils.Ok(t, err) testutils.Equals(t, 1, len(listings.Listings)) testutils.Equals(t, "http://www.somemls.com/lisings/1234567890", listings.Listings[0].ListingURL) testutils.Equals(t, "New Light Fixtures", *listings.Listings[0].Photos[1].Caption) testutils.Equals(t, "1100.0", listings.Listings[0].Expenses[2].Value.Value) }