class REXML::Light::Node
表示一个带标签的 XML 元素。 Elements
的特点是拥有子元素、属性和名称,并且它们本身也可以是子元素。
常量
- NAMESPLIT
- PARENTS
公共类方法
new(node=nil) 点击以切换源码
创建一个新元素。
# File rexml-3.4.0/lib/rexml/light/node.rb, line 13 def initialize node=nil @node = node if node.kind_of? String node = [ :text, node ] elsif node.nil? node = [ :document, nil, nil ] elsif node[0] == :start_element node[0] = :element elsif node[0] == :start_doctype node[0] = :doctype elsif node[0] == :start_document node[0] = :document end end
公共实例方法
<<(element) 点击以切换源码
将一个子元素附加到此元素,可以选择在提供的命名空间下。如果 element 参数是一个 Element
对象,则忽略 namespace 参数。否则,如果 element 参数是一个字符串,则 namespace(如果提供)是创建元素所在的命名空间。
# File rexml-3.4.0/lib/rexml/light/node.rb, line 114 def << element if node_type() == :text at(-1) << element else newnode = Node.new( element ) newnode.parent = self self.push( newnode ) end at(-1) end
=~( path ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 90 def =~( path ) XPath.match( self, path ) end
[]( reference, ns=nil ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 78 def []( reference, ns=nil ) if reference.kind_of? String pfx = '' pfx = "#{prefix(ns)}:" if ns at(3)["#{pfx}#{reference}"] elsif reference.kind_of? Range _old_get( Range.new(4+reference.begin, reference.end, reference.exclude_end?) ) else _old_get( 4+reference ) end end
[]=( reference, ns, value=nil ) 点击以切换源码
尚未处理命名空间
# File rexml-3.4.0/lib/rexml/light/node.rb, line 95 def []=( reference, ns, value=nil ) if reference.kind_of? String value = ns unless value at( 3 )[reference] = value elsif reference.kind_of? Range _old_put( Range.new(3+reference.begin, reference.end, reference.exclude_end?), ns ) else if value _old_put( 4+reference, ns, value ) else _old_put( 4+reference, ns ) end end end
children() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 143 def children self end
each() { |at(x+4)| ... } 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 36 def each size.times { |x| yield( at(x+4) ) } end
has_name?( name, namespace = '' ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 139 def has_name?( name, namespace = '' ) at(3) == name and namespace() == namespace end
local_name() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 54 def local_name namesplit @name end
local_name=( name_str ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 59 def local_name=( name_str ) _old_put( 1, "#@prefix:#{name_str}" ) end
name() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 40 def name at(2) end
name=( name_str, ns=nil ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 44 def name=( name_str, ns=nil ) pfx = '' pfx = "#{prefix(ns)}:" if ns _old_put(2, "#{pfx}#{name_str}") end
namespace( prefix=prefix() ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 67 def namespace( prefix=prefix() ) namespace_of( self, prefix ) end
namespace=( namespace ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 71 def namespace=( namespace ) @prefix = prefix( namespace ) pfx = '' pfx = "#@prefix:" if @prefix.size > 0 _old_put(1, "#{pfx}#@name") end
node_type() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 125 def node_type _old_get(0) end
parent() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 147 def parent at(1) end
parent=( node ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 50 def parent=( node ) _old_put(1,node) end
prefix( namespace=nil ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 63 def prefix( namespace=nil ) prefix_of( self, namespace ) end
root() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 134 def root context = self context = context.at(1) while context.at(1) end
size() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 28 def size if PARENTS.include? @node[0] @node[-1].size else 0 end end
text=( foo ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 129 def text=( foo ) replace = at(4).kind_of?(String)? 1 : 0 self._old_put(4,replace, normalizefoo) end
to_s() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 151 def to_s end
私有实例方法
namespace_of( node, prefix=nil ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 164 def namespace_of( node, prefix=nil ) if not prefix name = at(2) name =~ NAMESPLIT prefix = $1 end to_find = 'xmlns' to_find = "xmlns:#{prefix}" if not prefix.nil? ns = at(3)[ to_find ] ns ? ns : namespace_of( @node[0], prefix ) end
namesplit() 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 157 def namesplit return if @name.defined? at(2) =~ NAMESPLIT @prefix = '' || $1 @name = $2 end
prefix_of( node, namespace=nil ) 点击以切换源码
# File rexml-3.4.0/lib/rexml/light/node.rb, line 176 def prefix_of( node, namespace=nil ) if not namespace name = node.name name =~ NAMESPLIT $1 else ns = at(3).find { |k,v| v == namespace } ns ? ns : prefix_of( node.parent, namespace ) end end