# Libxml-ruby question

**URL:** <https://www.ruby-forum.com/t/libxml-ruby-question/174191>\
**Category:** Rails\
**Created:** [August 22, 2009, 3:40am UTC](https://www.ruby-forum.com/t/libxml-ruby-question/174191 "2009-08-22T03:40:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dekhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dekhaus](https://www.ruby-forum.com/u/dekhaus)\
**Post date:** [August 22, 2009, 3:40am UTC](https://www.ruby-forum.com/t/libxml-ruby-question/174191/1 "2009-08-22T03:40:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dekhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dekhaus](https://www.ruby-forum.com/u/dekhaus)\
**Post date:** [August 22, 2009, 11:03am UTC](https://www.ruby-forum.com/t/libxml-ruby-question/174191/2 "2009-08-22T11:03:24Z")

</div>

On Aug 22, 2:39 am, dekhaus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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.

Have you tried calling to\_s on the document object and returning  
that ?

Fred
