Extract a variable and add it to a separate XML file
<p>I have a script that logs in and returns a session ID. I need the session ID so that I can populate an XML file to do stuff with it, for example create a plant room for a nursery.</p>

<pre class="lang-perl prettyprint-override"><code>#!/usr/bin/perl -w

use strict;

use LWP::UserAgent;
use Data::Dumper;
use XML::Simple;

my $authenticate = do {
    open my $fh, '&lt;', '/tmp/login.xml' or die "Could not open file: $!";
    local $/;
    &lt;$fh&gt;;
};

my $webpage = "https://www.example.com/api";

my $ua = LWP::UserAgent-&gt;new;
my $response = $ua-&gt;post($webpage, Content_Type =&gt; 'text/xml',
                                        Content =&gt; $authenticate);


if ( $response-&gt;is_success) {

    my $xml = new XML::Simple;
    my $x = $response-&gt;decoded_content;

    # read XML file
    my $data = $xml-&gt;XMLin($x);

    my $sessionid =  $data-&gt;{'sessionid'};

...
</code></pre>

<p>I need to take that sessionid variable and insert into another xml file that looks like this:</p>

<pre class="lang-xml prettyprint-override"><code>&lt;xml&gt;
  &lt;API&gt;4.0&lt;/API&gt;
  &lt;action&gt;plant_room_add&lt;/action&gt;
  &lt;enforce_rules_training&gt;0&lt;/enforce_rules_training&gt;
  &lt;id&gt;3&lt;/id&gt;
  &lt;location&gt;123456&lt;/location&gt;
  &lt;name&gt;Test1&lt;/name&gt;
  &lt;sessionid&gt;$sessionid&lt;/sessionid&gt;
  &lt;signature&gt;Hello World&lt;/signature&gt;
  &lt;terminal_id&gt;xxxxxxx&lt;/terminal_id&gt;
  &lt;training&gt;1&lt;/training&gt;
&lt;/xml&gt;
</code></pre>

<p>I know that I can put the <code>plant_root_add</code> XML in the same script, but I think this script is growing to grow over time so I might need a script to delete the plant room. This is for monitoring and it will be executed every 60 seconds.</p>

<p>What is the way to do this?</p>