我正在尝试输出一个xml文件blog.xml作为yaml,用于放入vision.app,一个用于在本地设计购物电子商务网站的工具。
Shopify的yaml如下所示:
- id: 2
handle: bigcheese-blog
title: Bigcheese blog
url: /blogs/bigcheese-blog
articles:
- id: 1
title: 'One thing you probably did not know yet...'
author: Justin
content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
created_at: 2005-04-04 16:00
comments:
-
id: 1
author: John Smith
email: john@smith.com
content: Wow...great article man.
status: published
created_at: 2009-01-01 12:00
updated_at: 2009-02-01 12:00
url: ""
-
id: 2
author: John Jones
email: john@jones.com
content: I really enjoyed this article. And I love your shop! It's awesome. Shopify rocks!
status: published
created_at: 2009-03-01 12:00
updated_at: 2009-02-01 12:00
url: "http://somesite.com/"
- id: 2
title: Fascinating
author: Tobi
content: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
created_at: 2005-04-06 12:00
comments:
articles_count: 2
comments_enabled?: true
comment_post_url: ""
comments_count: 2
moderated?: true但是,示例myxml如下所示:
<article>
<author>Rouska Mellor</author>
<blog-id type="integer">273932</blog-id>
<body>Worn Again are hiring for a new Sales Director.
To view the full job description and details of how to apply click "here":http://antiapathy.org/?page_id=83</body>
<body-html><p>Worn Again are hiring for a new Sales Director.</p>
<p>To view the full job description and details of how to apply click <a href="http://antiapathy.org/?page_id=83">here</a></p></body-html>
<created-at type="datetime">2009-07-29T13:58:59+01:00</created-at>
<id type="integer">1179072</id>
<published-at type="datetime">2009-07-29T13:58:59+01:00</published-at>
<title>Worn Again are hiring!</title>
<updated-at type="datetime">2009-07-29T13:59:40+01:00</updated-at>
</article>
<article>我天真地假设从一种序列化数据格式到另一种数据格式的转换相当简单,我可以简单地这样做:
>> require 'hpricot'
=> true
>> b = Hpricot.XML(open('blogs.xml'))
>> puts b.to_yaml但是我得到了这个错误。
NoMethodError: undefined method `yaml_tag_subclasses?' for Hpricot::Doc:Class
from /usr/local/lib/ruby/1.8/yaml/tag.rb:69:in `taguri'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:16:in `to_yaml'
from /usr/local/lib/ruby/1.8/yaml.rb:391:in `call'
from /usr/local/lib/ruby/1.8/yaml.rb:391:in `emit'
from /usr/local/lib/ruby/1.8/yaml.rb:391:in `quick_emit'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:15:in `to_yaml'
from /usr/local/lib/ruby/1.8/yaml.rb:117:in `dump'
from /usr/local/lib/ruby/1.8/yaml.rb:432:in `y'
from (irb):6
from :0
>>如何获得此问题顶部列出的表单中的数据输出?我试着导入'yaml‘gem,认为我遗漏了其中的一些方法,但这也没有帮助:
发布于 2010-07-07 04:02:57
对不起,Josh,我认为你在这里找到的是Hpricot和/或YAML库中的一个限制,非常简单。
我不确定Hpricot是否曾经以这种方式支持YAML。所讨论的方法是由YAML库动态添加到Object类以及其他基本Ruby类型中的,但是由于某种原因没有出现在Hpricot::Doc的定义中,尽管Hpricot::Doc似乎确实间接地继承了Object。
我可以说我也复制了它,所以不只是你。
您可以很容易地添加缺少的方法:
class Hpricot::Doc
def self.yaml_tag_subclasses?
"true"
end
end
b = Hpricot.XML(open('blogs.xml'))但你会发现这并不能让你走得更远。下面是我得到的结果:
--- !ruby/object:Hpricot::Doc
options:
:xml: true所以我们没有像我们应该的那样在容器上迭代。
此时,要使用YAML库获得YAML支持,暴力方式(可能是唯一的方法)是将to_yaml方法添加到Hpricot的类中,以教它们如何正确地输出YAML。看一看“/usr/lib/ Ruby /1.8/yaml/rubytyes.rb”(在Mac上,这可能类似于"/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/yaml/rubytypes.rb"),以了解如何为每种基本的Ruby类型做这件事。您可能需要将其添加到的类是在C端定义的:请参阅方法Init_hpricot_scan中的“hpricot/ext/hpricot/hpricotscan.rl”。
发布于 2010-07-05 07:19:25
我找到了这个。也许这能帮上忙。http://brains.parslow.net/node/1623
https://stackoverflow.com/questions/1289074
复制相似问题