REXML: Equivalent of javascript-DOM's .innerHTML=
Is there a way to pass a string to an REXML::Element
in such a way th开发者_开发百科at the string will be parsed as XML, and the elements so found inserted into the target?
You can extend the REXML::Element class to include innerHTML as shown below.
require "rexml/element"
class REXML::Element
def innerHTML=(xml)
require "rexml/document"
self.to_a.each do |e|
self.delete e
end
d = REXML::Document.new "<root>#{xml}</root>"
d.root.to_a.each do |e|
case e
when REXML::Text
self.add_text e
when REXML::Element
self.add_element e
else
puts "ERROR"
end
end
xml
end
def innerHTML
ret = ''
self.to_a.each do |e|
ret += e.to_s
end
ret
end
end
You can then use innerHTML as you would in javascript (more or less).
require "rexml/document"
doc = REXML::Document.new "<xml><alice><b>bob</b><chuck>ch<u>u</u>ck</chuck></alice><alice/></xml>"
c = doc.root.get_elements('//chuck').first
t = c.innerHTML
c.innerHTML = "#{t}<david>#{t}</david>"
c = doc.root.get_elements('//alice').last
c.innerHTML = "<david>#{t}</david>"
doc.write( $stdout, 2 )
It would help if you could provide an example to further illustrate exactly what you had in mind.
With JS innerHTML you can insert text or HTML in one shot and changes are immediately displayed in the HTML document. The only way I know how to do this in REXML is with separate steps for inserting content/elements and saving/reloading the document.
To modify the text of a specific REXML Elemement you can use the text=() method.
#e represents a REXML Element
e.text = "blah"
If you want to insert another element you have to use the add_element() method.
#e represents a REXML Element
e.add_element('blah') #adds <blah></blah> to the existing element
b = e.get_elements('blah') #empty Element named "blah"
b.text('some text') #add some text to Element blah
Then of course save the XML document with the changes. ruby-doc.org/REXML/Element
text()
will return the inner content as a string
精彩评论