Class: Rexle
Defined Under Namespace
Classes: CData, Element, Elements
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from XMLhelper
#doc_pretty_print, #doc_print, #pretty_print, #processing_instructions, #scan_print, #scan_to_a
Constructor Details
#initialize(x = nil) ⇒ Rexle
Returns a new instance of Rexle.
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/rexle.rb', line 218
def initialize(x=nil)
super()
@instructions = [["xml", "version='1.0' encoding='UTF-8'"]]
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
#instructions ⇒ Object
Returns the value of attribute instructions.
216
217
218
|
# File 'lib/rexle.rb', line 216
def instructions
@instructions
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
215
216
217
|
# File 'lib/rexle.rb', line 215
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
926
|
# File 'lib/rexle.rb', line 926
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
930
931
932
933
934
935
936
937
938
939
940
941
|
# File 'lib/rexle.rb', line 930
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
943
|
# File 'lib/rexle.rb', line 943
def add_text(s) end
|
#attribute(key) ⇒ Object
927
|
# File 'lib/rexle.rb', line 927
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
928
|
# File 'lib/rexle.rb', line 928
def attributes() @doc.attributes end
|
#content(options = {}) ⇒ Object
991
992
993
|
# File 'lib/rexle.rb', line 991
def content(options={})
CGI.unescapeHTML(xml(options))
end
|
#delete(xpath) ⇒ Object
Also known as:
remove
947
948
949
950
951
|
# File 'lib/rexle.rb', line 947
def delete(xpath)
e = @doc.element(xpath)
e.delete if e
end
|
#element(xpath) ⇒ Object
955
|
# File 'lib/rexle.rb', line 955
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
956
|
# File 'lib/rexle.rb', line 956
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
957
|
# File 'lib/rexle.rb', line 957
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
|
# File 'lib/rexle.rb', line 906
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',nil,{}]
@a = procs[x.class.to_s.to_sym].call(x)
@doc = scan_element(*(doc_node << @a))
self
end
|
#root ⇒ Object
966
|
# File 'lib/rexle.rb', line 966
def root() @doc.elements.first end
|
#text(xpath) ⇒ Object
965
|
# File 'lib/rexle.rb', line 965
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
958
|
# File 'lib/rexle.rb', line 958
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
960
961
962
963
|
# File 'lib/rexle.rb', line 960
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
968
969
970
|
# File 'lib/rexle.rb', line 968
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
|
# File 'lib/rexle.rb', line 972
def xml(options={})
return '' unless @doc
o = {pretty: false, declaration: true}.merge(options)
msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
r = ''
if o[:declaration] == true then
unless @instructions.assoc 'xml' then
@instructions.unshift ["xml","version='1.0' encoding='UTF-8'"]
end
end
r << method(msg).call(self.root.children, o[:declaration]).strip
r
end
|
#xpath(path, &blk) ⇒ Object
250
251
252
|
# File 'lib/rexle.rb', line 250
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|