Previous | Next | WireHose Developers Guide |
Now you'll start building Hello World's importer. This class will use the WHImporter utility class to do most of its heavy lifting.
import com.wirehose._util.*; import com.wirehose.base.*;
public static void importFeeds() { // fetch feeds list as dictionary NSMutableDictionary rss = WHImporter.fetchDictionaryFromURL( "SampleRSSFeeds.xml", "Contents/Resources/rss20MappingModel.xml"); // extract feeds from dictionary and clean up tags NSMutableArray snapshots = cleanSnapshots(rss.valueForKeyPath("channel.items")); EOEditingContext ec = new EOEditingContext(); ec.lock(); try { // insert resources into editing context WHImporter.insertResources( ec, snapshots, "RSSFeed", "Feeds/", null, WHImporter.IgnoreAndTag, true, true, true, true, false); ec.saveChanges(); } catch (Exception e) { System.out.println("Error importing resources: "+e); e.printStackTrace(); } try { ec.saveChanges(); } catch (Exception e) { System.out.println("Exception saving changes: "+e); } ec.unlock(); ec.dispose(); }
This method uses two methods from WHImporter. The first,
fetchDictionaryFromURL,
takes two arguments which
specify the location of an XML file to be imported, and the
mapping model which will turn the XML into a dictionary.
The other WHImporter method, insertResources
, is a
general-purpose utility for inserting resources into a database.
It takes several arguments which specify how to handle resources
which already exist in the database, whether or not to index
keywords for newly inserted resources, whether to add tags to
resources, etc.
The arguments used here ensure that if a new resource already exists in the database, the new resource will be ignored rather than inserted. If any tags are specified on the new resource, those tags will be assigned to the already existing resource. This handles the case where an identical resource has been imported multiple times in multiple categories, by having only a single resource with multiple tags.
The "Feeds/" argument specifies a tag path prefix. Any categories described in the feed will be appended to this string before being turned into tags. For example, if a feed has an assigned category of "Consumer/Gardening", the tag used will actually be "Feeds/Consumer/Gardening".
Copyright ©2000-2003 Gary Teter. All rights reserved. WireHose is a trademark of Gary Teter.