Hi
This has been driving me nuts for the past hour or two. I'm hoping someone here knows the answer.
I need to be able to dynamically add some nodes to some xml. The example below is contrived - but hopefully clear enough so you get the 'picture'
So - say I have a Foo object and I want to add something to a child node within Foo's xml representation before I send the response back to the user.
In my controller ...
def show @foo = Foo.find(params[:id])
respond_to do |format| format.html # show.html.erb format.xml { render :xml => extend_foo(@foo.to_xml) } end end
private
def extend_foo(xml)
parser = XML::Parser.new(XML::Parser::Context.string(xml)) doc = parser.parse
child_node_xpath = doc.find(" .... insert funky xpath string here ....")
child_node = child_node_xpath.first
logger.debug("\n\nbefore ...\n#{child_node.to_s}\n\n") # this correctly outputs the contents of the 'child_node'
child_node << child = XML::Node.new('new_node') child['new_node_attribute'] = 'true'
logger.debug("\n\nafter ...\n#{child_node.to_s}\n\n") # this correctly outputs the contents of the 'child_node' - with the 'new_node' added (along with the 'new_node_attribute')
# NOTE: everything up to here works great.
end
The problem is that the updated node is not part of the response sent to the client. For lake of a better term - I can't seem to figure out how to 'save' the changed node.
Any tips/suggestions/pointers would be greatly appreciated. I'm using libxml.
Thanks Dave