4

xml ファイル blog.xml を yaml として出力しようとしています。これは、shopify e コマース サイトをローカルで設計するためのツールである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 &quot;here&quot;:http://antiapathy.org/?page_id=83</body>
          <body-html>&lt;p&gt;Worn Again are hiring for a new Sales Director.&lt;/p&gt;
      &lt;p&gt;To view the full job description and details of how to apply click &lt;a href=&quot;http://antiapathy.org/?page_id=83&quot;&gt;here&lt;/a&gt;&lt;/p&gt;</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」ジェムをインポートしようとしましたが、どちらも役に立ちませんでした:

4

2 に答える 2

1

申し訳ありませんが、ジョシュ、あなたがここで見つけたのは、Hpricot および/または YAML ライブラリの純粋で単純な制限だと思います。

Hpricot がこの方法で YAML をサポートしたことがあるかどうかはわかりません。問題のメソッドは、YAML ライブラリによって Object クラスや他の基本的な Ruby 型に動的に追加されますが、Hpricot::Doc は継承しているように見えますが、何らかの理由で Hpricot::Doc の定義には表示されません。オブジェクトから間接的に。

私も再現したと言えるので、あなただけではありません。

不足しているメソッドを非常に簡単に追加できます。

class Hpricot::Doc
  def self.yaml_tag_subclasses?
    "true"
  end
end
b = Hpricot.XML(open('blogs.xml'))

しかし、それだけでは先に進めないことがわかります。ここに私が得るものがあります:

--- !ruby/object:Hpricot::Doc 
options: 
  :xml: true

そのため、必要なようにコンテナーを反復処理していません。

to_yamlこの時点で、YAML ライブラリを使用して YAML サポートを取得するには、Hpricot のクラスにメソッドを追加して、YAML を正しく出力する方法を教えるという強引な方法 (おそらく唯一の方法) があります。「/usr/lib/ruby/1.8/yaml/rubytypes.rb」を見てください (Mac では、「/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib」のようになります)。 /ruby/1.8/yaml/rubytypes.rb") で、Ruby の基本的な型ごとにそれがどのように行われるかの例を参照してください。これを追加する必要があるかもしれないクラスは、C 側で定義されていInit_hpricot_scanます。

于 2010-07-06T20:02:56.567 に答える
1

私はこれを見つけました。多分それは役立つかもしれません。 http://brains.parslow.net/node/1623

于 2010-07-04T23:19:24.580 に答える