Class: Rexle
Defined Under Namespace
Classes: Element, Elements
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from XMLhelper
#doc_pretty_print, #doc_print, #pretty_print, #scan_print, #scan_to_a
Constructor Details
#initialize(x = nil) ⇒ Rexle
Returns a new instance of Rexle.
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/rexle.rb', line 158
def initialize(x=nil)
super()
if x then
procs = {
String: proc {|x| parse_string(x)},
Array: proc {|x| x},
:"REXML::Document" => proc {|x| scan_doc x.root}
}
doc_node = ['doc','',{}]
@a = procs[x.class.to_s.to_sym].call(x)
@doc = scan_element(*(doc_node << @a))
@prefixes = []
if @doc.root.attributes then
xmlns = @doc.root.attributes.select {|k,v| k[/^xmlns:/]}
@prefixes = xmlns.keys.map{|x| x[/\w+$/]}
end
end
end
|
Instance Attribute Details
#prefixes ⇒ Object
Returns the value of attribute prefixes.
156
157
158
|
# File 'lib/rexle.rb', line 156
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
793
|
# File 'lib/rexle.rb', line 793
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
797
798
799
800
801
802
803
804
805
806
|
# File 'lib/rexle.rb', line 797
def add_element(element)
if @doc then
raise 'attempted adding second root element to document' if @doc.root
@doc.root.add_element(element)
else
doc_node = ['doc', '', {}, element.to_a]
@doc = scan_element(*doc_node)
end
element
end
|
#add_text(s) ⇒ Object
808
|
# File 'lib/rexle.rb', line 808
def add_text(s) end
|
#attribute(key) ⇒ Object
794
|
# File 'lib/rexle.rb', line 794
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
795
|
# File 'lib/rexle.rb', line 795
def attributes() @doc.attributes end
|
#delete(xpath) ⇒ Object
Also known as:
remove
812
813
814
815
|
# File 'lib/rexle.rb', line 812
def delete(xpath)
e = @doc.element(xpath)
e.delete if e
end
|
#element(xpath) ⇒ Object
819
|
# File 'lib/rexle.rb', line 819
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
820
|
# File 'lib/rexle.rb', line 820
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
821
|
# File 'lib/rexle.rb', line 821
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
|
# File 'lib/rexle.rb', line 773
def parse(x=nil)
a = []
if x then
procs = {
String: proc {|x| parse_string(x)},
Array: proc {|x| x},
:"REXML::Document" => proc {|x| scan_doc x.root}
}
a = procs[x.class.to_s.to_sym].call(x)
else
a = yield
end
doc_node = ['doc','',{}]
@a = procs[x.class.to_s.to_sym].call(x)
@doc = scan_element(*(doc_node << @a))
self
end
|
#root ⇒ Object
830
|
# File 'lib/rexle.rb', line 830
def root() @doc.elements.first end
|
#text(xpath) ⇒ Object
829
|
# File 'lib/rexle.rb', line 829
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
822
|
# File 'lib/rexle.rb', line 822
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
824
825
826
827
|
# File 'lib/rexle.rb', line 824
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
832
833
834
|
# File 'lib/rexle.rb', line 832
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
836
837
838
839
840
841
842
843
844
845
846
847
|
# File 'lib/rexle.rb', line 836
def xml(options={})
return '' unless @doc
o = {pretty: false, declaration: true}.merge(options)
msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
r = ''
r = "<?xml version='1.0' encoding='UTF-8'?>\n" if o[:declaration] == true
r << method(msg).call(self.root.children)
r
end
|
#xpath(path, &blk) ⇒ Object
188
189
190
|
# File 'lib/rexle.rb', line 188
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|