Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: best way to change xml record using XML::Simple?

by BrowserUk (Patriarch)
on Feb 24, 2003 at 21:24 UTC ( [id://238239]=note: print w/replies, xml ) Need Help??


in reply to best way to change xml record using XML::Simple?

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.

Replies are listed 'Best First'.
care to explain __DATA__?
by waxmop (Beadle) on Feb 24, 2003 at 21:55 UTC
    How does the __DATA__ block work? Is that some sort of handle? Any links to explanatory would be much appreciated.

      From perldata:

      The two control characters ^D and ^Z, and the tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored.

      Text after __DATA__ but may be read via the filehandle "PACKNAME::DATA", where "PACKNAME" is the package that was current when the __DATA__ token was encountered. The filehandle is left open pointing to the contents after __DATA__. It is the program's responsibility to "close DATA" when it is done reading from it. For compatibility with older scripts written before __DATA__ was introduced, __END__ behaves like __DATA__ in the toplevel script (but not in files loaded with "require" or "do") and leaves the remaining contents of the file accessible via "main::DATA".

      See SelfLoader for more description of __DATA__, and an example of its use. Note that you cannot read from the DATA filehandle in a BEGIN block: the BEGIN block is executed as soon as it is seen (during compilation), at which point the corresponding __DATA__ (or __END__) token has not yet been seen.

      After Compline,
      Zaxo

      I can't find a reference to __DATA__ in the docs. I know its there somehere. Maybe someone else will post one.

      Essentially, you can use <DATA> as a file handle that you don't need to open to access anything after the __DATA__ marker at the end of your source file, __END__ works too, but has caveats when used with modules I was informed recently.

      It's very useful for testing and demo purposes. You can even have multiple embedded and even writable files using Damian Conway's Inline::Files, I don't think they would be useful for your purposes here though.


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

        perldoc perldata, about halfway down. Also, in the camel on pages 68 and 660.

        --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://238239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found