title: Adding new child nodes to an existing XML file with Ruby & Nokogiri
body: <p>I have this server project in Ruby, and I would like to keep tracks of events and user sessions in a XML file. I'm totally new to this, and after days of research, I'm hitting a wall.</p>
  
  <p>Here's my current sample code, assuming there's already a file named "test.xml" that contains a root node called </p>
  
  <pre><code>$ cat test.xml
  &lt;server&gt;&lt;/server&gt;
  </code></pre>
  
  <p>and the code :</p>
  
  <pre><code>require 'nokogiri'
  require 'securerandom'
  
  logintime = Time.now
  sessionid = SecureRandom.hex(10)
  file = File.open("test.xml",'a+')
  doc = Nokogiri::XML.parse file
  session_node = Nokogiri::XML::Node.new("session",doc)
  session_node['id'] = sessionid
  logintime_node = Nokogiri::XML::Node.new("logintime",doc)
  logintime_node.content = logintime
  session_node &lt;&lt; logintime_node
  doc.root &lt;&lt; session_node
  file.print doc.to_xml
  file.close
  </code></pre>
  
  <p>and here's the test.xml file after 4 runs</p>
  
  <pre><code>&lt;server&gt;&lt;/server&gt;
  &lt;?xml version="1.0"?&gt;
  &lt;server&gt;
    &lt;session id="5ef27ade2afaf5c2162f"&gt;
      &lt;logintime&gt;2015-07-07 17:27:20 +0200&lt;/logintime&gt;
    &lt;/session&gt;
  &lt;/server&gt;
  &lt;?xml version="1.0"?&gt;
  &lt;server&gt;
    &lt;session id="637595bd0857c8af1cc0"&gt;
      &lt;logintime&gt;2015-07-07 17:27:36 +0200&lt;/logintime&gt;
    &lt;/session&gt;
  &lt;/server&gt;
  &lt;?xml version="1.0"?&gt;
  &lt;?xml version="1.0"?&gt;
  &lt;server&gt;
    &lt;session id="41e6082c4db7d1dc8692"&gt;
      &lt;logintime&gt;2015-07-07 17:27:37 +0200&lt;/logintime&gt;
    &lt;/session&gt;
  &lt;/server&gt;
  &lt;?xml version="1.0"?&gt;
  &lt;?xml version="1.0"?&gt;
  &lt;server&gt;
    &lt;session id="1cad6c3d38d4fb96632b"&gt;
      &lt;logintime&gt;2015-07-07 17:27:38 +0200&lt;/logintime&gt;
    &lt;/session&gt;
  &lt;/server&gt;
  &lt;?xml version="1.0"?&gt;
  </code></pre>
  
  <p>And the desired output should be something like this :</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;server&gt;
    &lt;session id="5ef27ade2afaf5c2162f"&gt;
      &lt;logintime&gt;2015-07-07 17:27:20 +0200&lt;/logintime&gt;
    &lt;/session&gt;
    &lt;session id="637595bd0857c8af1cc0"&gt;
      &lt;logintime&gt;2015-07-07 17:27:36 +0200&lt;/logintime&gt;
    &lt;/session&gt;
    &lt;session id="41e6082c4db7d1dc8692"&gt;
      &lt;logintime&gt;2015-07-07 17:27:37 +0200&lt;/logintime&gt;
    &lt;/session&gt;
    &lt;session id="1cad6c3d38d4fb96632b"&gt;
      &lt;logintime&gt;2015-07-07 17:27:38 +0200&lt;/logintime&gt;
    &lt;/session&gt;
  &lt;/server&gt;
  </code></pre>
  
  <p>And I really don't know why should I do to obtain that result.</p>
  
  <p>First, if there's no existing file containing the  root node, the script run only once, then complains that there's already a root node when I try to run it a second time :</p>
  
  <pre><code>/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/document.rb:232:in `add_child': Document already has a root node (RuntimeError)
      from /Users/xxx/nokogiri.rb:13:in `&lt;top (required)&gt;'
      from -e:1:in `load'
      from -e:1:in `&lt;main&gt;'
  </code></pre>
  
  <p>So... I'm kinda lost here. Any ideas ?</p>