Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Writing Out XML using XML::Simple

by Anonymous Monk
on Apr 07, 2003 at 11:23 UTC ( #248561=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Can anyone tell me how I can use XMLout() from XML::Simple to write out XML to an external foo.xml file.

My foo.xml file looks like this

<profile username="Tom" language="eng" lastlogin=""> <bids> <itemcode>4985874875</itemcode> <itemcode>7685976785</itemcode> <itemcode>6758679837</itemcode> <itemcode>7849609576</itemcode> <itemcode>2857689576</itemcode> </bids> <itemwatch> <itemcode>6758767856</itemcode> <itemcode>3758678576</itemcode> </itemwatch> </profile>

I have this code to read in the XML file.

#!c:\perl\bin\perl -w use XML::Simple; use CGI::Carp 'fatalsToBrowser'; my $config = XMLin(); print "content-type:text/html\n\n"; print "<h1>Then</h1>"; foreach (@{$config->{bids}->{itemcode}}) { print $_, "<br/>"; }

I then have this code to add a new value to the hash ref thing and then try to write this out again using XMLout() but it doesn't work... what am I doing wrong? Do I need to use Storable module, or maybe just open a file handle and do it manually...?!

push @{$config->{bids}->{itemcode}}, 'newcode43545'; print "<h1>Now</h1>"; foreach (@{$config->{bids}->{itemcode}}) { print $_, "<br/>"; } XMLout($config);

Thanks!
Tom

Replies are listed 'Best First'.
Re: Writing Out XML using XML::Simple
by valdez (Monsignor) on Apr 07, 2003 at 11:48 UTC

    XMLout will not save the file for you, it will provide only an xml representation of your data structure; try to add a print before XMLout and you'll see the result. This is explained clearly in XML::Simple man page.

    Ciao, Valerio

(jeffa) Re: Writing Out XML using XML::Simple
by jeffa (Bishop) on Apr 07, 2003 at 14:15 UTC
    I had no problem getting your code to output the new XML, using valdez's advice. The reason i am posting is because your question states that you want to create a new XML file, yet you are also outputing HTML ... if all you need to do is print out the item codes, you can use XML::XPath, which i find is a better tool for this kind of problem:
    use strict; use warnings; use XML::XPath; use CGI::Pretty qw(:standard); my $xpath = XML::XPath->new(ioref => *DATA); my $nodeset = $xpath->find('/profile/bids/itemcode'); my @b_item = map $_->string_value, $nodeset->get_nodelist; print header, start_html('itemcodes'), h1('Then'), (map { $_,br } @b_item), end_html, ; __DATA__ <profile username="Tom" language="eng" lastlogin=""> <bids> <itemcode>4985874875</itemcode> <itemcode>7685976785</itemcode> <itemcode>6758679837</itemcode> <itemcode>7849609576</itemcode> <itemcode>2857689576</itemcode> </bids> <itemwatch> <itemcode>6758767856</itemcode> <itemcode>3758678576</itemcode> </itemwatch> </profile>
    One last note, i rarely have to add anything to the XML files that i use. Instead, data is typically stored somewhere else (a database usually) and i use XML as an intermediary transformation. This way, the need to edit an XML file is aleviated, instead, i change the data in the database and create a new XML file. This doesn't always work, but it seems like a much better way to go in the long run ... YMMV, of course. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Thanks for the reply, I bascially just want to be able to read in an XML file, display it's contents to the user. Then be able to ammend the contents.

      You see i'm trying to store each users profile information in an XML file, so they will need to be able to view the info, but also change and add to it.

      The XPath code looks very useful, cheers!

        You are most welcome. :)

        Now, is there any real compelling reason to store this info in an XML file? I really don't see one. In the past, i have used XML as a storage medium, but that was for my ease, no one else was using my code.

        If your users need to be able to change the data, then i think you are in for a minor headache if you choose XML. I highly recommend you use an relational database instead. Why not start with DBD::SQLite? It takes care of concurrency issues for, you have to write the code to do that yourself if you use a text file. Also, by using SQLite (or mysql, or Postgres, etc.), you can always convert your data to XML with tools like XML::Generator::DBI.

        Of course, if you don't already know SQL, you will have to spend some time learning it ... but it is time well spent, SQL can take you a looong way. ;) Good luck!

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Writing Out XML using XML::Simple
by AcidHawk (Vicar) on Apr 07, 2003 at 14:08 UTC

    You can try something like this.

    my $file = "newfoo.xml"; open CREATE, ">$file") or die "Cannot create XML File: $!"; my $xsimple = XML::Simple->new(); print CREATE $xsimple->XMLout($config, noattr => 1, xmldecl => '<?xml version="1.0"?>'); close(CREATE);

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

      yeah thanks that was just what I was looking for!

      It messes up my XML a little bit but I guess the meaning remains the same. I think i'll play around the the options to try and keep things the way I want them, thanks though.

Re: Writing Out XML using XML::Simple
by grantm (Parson) on Apr 07, 2003 at 18:57 UTC

    If you want XML::Simple to write the XML out to a file rather than returning a string, then you could use the 'outputfile' option:

    XMLout($config, outputfile => 'your_filename_here');

    Also, I notice you're not specifying any options on your call to XMLin(). I would think you should be using at least:

    my $config = XMLin(undef, forcearray => [ 'item'code ]);

    For more details of why you don't want to rely on the defaults, see this node.

Re: Writing Out XML using XML::Simple
by CodeJunkie (Monk) on Apr 07, 2003 at 11:26 UTC
    That last post was by me, sorry forgot to login! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://248561]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-07-02 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?