rubyxmlrexml

REXML: Equivalent of javascript-DOM's .innerHTML=


Is there a way to pass a string to an REXML::Element in such a way that the string will be parsed as XML, and the elements so found inserted into the target?


Solution

  • 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