解析的 xml 文件:如果为空则跳过创建?
这可能是 HappyMapper 特定的问题,但我不这么认为。
在我的应用程序中,用户可以上传他们的博客订阅(通过 OPML 文件),我会解析这些内容并将其添加到他们的个人资料中。唯一的问题是在解析过程中,或者更具体地说在每个订阅的创建过程中,我无法弄清楚如何跳过只是“标签”的条目。
由于 OPML 文件允许您标记博客或将它们组织到文件夹中,这就是我的问题。实际的博客订阅及其标签都有“大纲”标签。
<outline text="Rails" >
<outline title="Katz Got Your Tongue?" text="Katz Got Your Tongue?" htmlUrl="http://yehudakatz.com" type="rss" xmlUrl="http://feeds.feedburner.com/KatzGotYourTongue" />
解析后,我通过 HappyMapper 模块内部的方法调用创建每个提要
def create_feed
Feed.new( :feed_htmlUrl => self.htmlUrl, :feed_title => self.title, ...
但是如何防止它为那些只是标签的大纲标签创建新的“提要”? (即那些没有 htmlUrl 的?)
This could be a HappyMapper specific question, but I don't think so.
In my app, users can upload their blog subscriptions (via an OPML file), which I parse and add to their profile. The only problem is during the parsing, or more specifically the creation of each subscription, I can't figure out how to skip over entries that are just "labels".
Since OPML files allow you to label your blogs, or organize them into folders, this is my problem. The actual blog subscriptions and their labels both have "outline" tags.
<outline text="Rails" >
<outline title="Katz Got Your Tongue?" text="Katz Got Your Tongue?" htmlUrl="http://yehudakatz.com" type="rss" xmlUrl="http://feeds.feedburner.com/KatzGotYourTongue" />
After parsing, I create each feed via a method call inside of the HappyMapper module
def create_feed
Feed.new( :feed_htmlUrl => self.htmlUrl, :feed_title => self.title, ...
But how do I prevent it from creating new "feeds" for those outline tags that are just tags? (i.e. those that don't have an htmlUrl?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将尝试这样做:
在
Feed
模型类中,我们使用before_create
过滤器。如下:这样做,只会创建满足 validate_attribute 的新记录。
希望它有帮助。
I will try this:
In the
Feed
model class, we use abefore_create
filter. As follows:Doing this, only new record that satisfy validate_attribute will be created.
Hopefully it helps.