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.
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/rexle.rb', line 266
def initialize(x=nil)
super()
@instructions = [["xml", "version='1.0' encoding='UTF-8'"]]
@doctype = :xml
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
#doctype ⇒ Object
Returns the value of attribute doctype.
263
264
265
|
# File 'lib/rexle.rb', line 263
def doctype
@doctype
end
|
#instructions ⇒ Object
Returns the value of attribute instructions.
264
265
266
|
# File 'lib/rexle.rb', line 264
def instructions
@instructions
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
263
264
265
|
# File 'lib/rexle.rb', line 263
def prefixes
@prefixes
end
|
Instance Method Details
#add_attribute(x) ⇒ Object
1076
|
# File 'lib/rexle.rb', line 1076
def add_attribute(x) @doc.attribute(x) end
|
#add_element(element) ⇒ Object
Also known as:
add
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
|
# File 'lib/rexle.rb', line 1080
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
1093
|
# File 'lib/rexle.rb', line 1093
def add_text(s) end
|
#at_css(selector) ⇒ Object
303
304
305
|
# File 'lib/rexle.rb', line 303
def at_css(selector)
@doc.root.element RexleCSS.new(selector).to_xpath
end
|
#attribute(key) ⇒ Object
1077
|
# File 'lib/rexle.rb', line 1077
def attribute(key) @doc.attribute(key) end
|
#attributes ⇒ Object
1078
|
# File 'lib/rexle.rb', line 1078
def attributes() @doc.attributes end
|
#clone ⇒ Object
299
300
301
|
# File 'lib/rexle.rb', line 299
def clone()
Rexle.new self.to_a
end
|
#content(options = {}) ⇒ Object
1141
1142
1143
|
# File 'lib/rexle.rb', line 1141
def content(options={})
CGI.unescapeHTML(xml(options))
end
|
#css(selector) ⇒ Object
307
308
309
|
# File 'lib/rexle.rb', line 307
def css(selector)
selector.split(',').flat_map{|x| @doc.root.xpath RexleCSS.new(x).to_xpath}
end
|
#delete(xpath) ⇒ Object
Also known as:
remove
1097
1098
1099
1100
1101
|
# File 'lib/rexle.rb', line 1097
def delete(xpath)
e = @doc.element(xpath)
e.delete if e
end
|
#element(xpath) ⇒ Object
1105
|
# File 'lib/rexle.rb', line 1105
def element(xpath) self.xpath(xpath).first end
|
#elements(s = nil) ⇒ Object
1106
|
# File 'lib/rexle.rb', line 1106
def elements(s=nil) @doc.elements(s) end
|
#name ⇒ Object
1107
|
# File 'lib/rexle.rb', line 1107
def name() @doc.root.name end
|
#parse(x = nil) ⇒ Object
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
|
# File 'lib/rexle.rb', line 1056
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
1116
|
# File 'lib/rexle.rb', line 1116
def root() @doc.elements.first end
|
#text(xpath) ⇒ Object
1115
|
# File 'lib/rexle.rb', line 1115
def text(xpath) @doc.text(xpath) end
|
#to_a ⇒ Object
1108
|
# File 'lib/rexle.rb', line 1108
def to_a() @a end
|
#to_s(options = {}) ⇒ Object
1110
1111
1112
1113
|
# File 'lib/rexle.rb', line 1110
def to_s(options={})
return '<UNDEFINED/>' unless @doc
self.xml options
end
|
#write(f) ⇒ Object
1118
1119
1120
|
# File 'lib/rexle.rb', line 1118
def write(f)
f.write xml
end
|
#xml(options = {}) ⇒ Object
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
|
# File 'lib/rexle.rb', line 1122
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
311
312
313
|
# File 'lib/rexle.rb', line 311
def xpath(path, &blk)
@doc.xpath(path, &blk)
end
|