NULL::something

Login via flickr, はてな, TypeKey.

2004-10-22

REXML, SourceFactory, Element

これ全く使えないんですけれど、何か他にモジュールとか、もしくはバージョン制限とかあるんですかね。

p REXML::Version
#=> "3.1.2" or "2.7.3"
src = REXML::SourceFactory.create_from("<a attr='val'>text<b/></a>")
puts (e = REXML::Element.new(src))
#=> </>
puts REXML::Element.new("child", e)
#=> <child/>
puts e
#=> <><child/></>
puts REXML::Element.new(e)
#=> </>

これは 私家版REXML APIリファレンス のやつをそのままコピって実行してみた結果。

自力でエレメンツちまちま追加してくとか DOM 並に面倒臭いのですが!

rexml/element.rb の initialize に Source クラスのインスタンス渡したときの処理が 3.1.2 でも 2.7.3 でも見当たらない。コメントにはちゃんと書いてあるんだけど、未実装なのかな。

それと REXML::Element.new("<ele>") の結果が <<ele>/> なのはどうなのよ。使用できない文字渡されたら例外のほうがいいと思うんだけど。

Document と同じ build メソッドを作ってやればいいのかな。あとで試そう。

Inserted at 2004-10-22T20:41:32+09:00

module REXML
    class Element
        def initialize( arg = UNDEFINED, parent=nil, context=nil )
            super(parent)

            @elements = Elements.new(self)
            @attributes = Attributes.new(self)
            @context = context

            if arg.kind_of? String
                self.name = arg
            elsif arg.kind_of? Element
                self.name = arg.expanded_name
                arg.attributes.each_attribute{ |attribute|
                    @attributes << Attribute.new( attribute )
                }
                @context = arg.context
            elsif arg.kind_of? Source
                build arg
                self.name = @elements[1].expanded_name
                @attributes = @elements[1].attributes
                @elements = @elements[1].elements
                @children = @children[0].instance_eval("@children")
            end
        end

        private
        def build(source)
            Parsers::TreeParser.new(source, self).parse
        end

    end
end

無理やりなんだけどこれでいいのかなぁ。表示的には思い通りだけど何か穴がありそうだ……

Inserted at 2004-10-22T21:58:26+09:00

Comments (0)

Trackback URI: http://lowreal.net/logs/2004/10/22/2.trackback

NULL

RSS feed meter for http://lowreal.net/logs/latest

Alternates

  1. RSS 1.0
  2. Atom 1.0

Generated with Taglibro

この日記は Taglibro と呼ばれる XML ベースの XSLT をテンプレートとして使ったシステムを使っています。現在の Taglibro は Ruby, ruby-xslt, libxml-ruby による実装です。ソースコードはとりあえず公開していません。