If you play with the attributes on XMLin() and XMLout() your can avoid needing to search by forcing it to use hashes instead of arrays.

Using keeproot=>1, keyattr=>'uid' on both the XMLin() and XMLout() calls and adding noattr=>1 on the XMLout() you can pursuade XML::Simple to write the data back in the same form as it received it.

If you can't use a DB of some form, and you expect to have multiple concurrent users, then you could move the access of the data into a seperate process, maintaining a copy in memory and have it serve details to, and receive updates from the CGI process via a socket or pipe. This would probably need to be multi-threaded/forked but you might get away with having the CGI try to connect and then backoff for a short period and retry if it doesn't get access first time, if the volumes of traffic are low.

#! perl -slw use strict; use Data::Dumper; my %ARGS; @ARGS{"uid", "company_name", "contact_name", "contact_phone_number"} = ("0001", "Acme Industries", "Arthur Dent", 555-1234 ); #<%init> use XML::Simple; my $xref = XMLin( join('',<DATA>), keyattr=>'uid', keeproot=>1 ); my $newnode = {}; foreach my $kee ( "uid", "company_name", "contact_name", "contact_phon +e_number" ) { $newnode->{$kee} = $ARGS{$kee}; } $xref->{records}{record}{$ARGS{uid}} = { "company_name" =>$ARGS{"company_name"}, "contact_name"=>$ARGS{"contact_name"}, "contact_phone_number"=>$ARGS{"contact_phone_number"}, }; print XMLout( $xref, keeproot=>1, noattr=>1, keyattr=>'uid' ); #</%init> __DATA__ <records> <record> <uid>0001</uid> <company_name>Acme Industries</company_name> <contact_name>Arthur Dent</contact_name> <contact_phone_number>867-5309</contact_phone_number> </record> <record> <uid>0002</uid> <company_name>Zeta Industries</company_name> <contact_name>Sam Lowry</contact_name> <contact_phone_number>555-5555</contact_phone_number> </record> </records>

the output

<records> <record> <contact_name>Sam Lowry</contact_name> <contact_phone_number>555-5555</contact_phone_number> <company_name>Zeta Industries</company_name> <uid>0002</uid> </record> <record> <contact_name>Arthur Dent</contact_name> <contact_phone_number>-679</contact_phone_number> <company_name>Acme Industries</company_name> <uid>0001</uid> </record> </records>

..and remember there are a lot of things monks are supposed to be but lazy is not one of them

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: best way to change xml record using XML::Simple? by BrowserUk
in thread best way to change xml record using XML::Simple? by waxmop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.