in reply to Writing Out XML using XML::Simple

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)

Replies are listed 'Best First'.
Re: (jeffa) Re: Writing Out XML using XML::Simple
by CodeJunkie (Monk) on Apr 07, 2003 at 14:51 UTC

    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)
      

        Ok thanks for the advice again. I'm really just using XML files because I thought it would be cool and i'm fed up with using CSV files. I also want to learn something new and I thought this was a good reason to try. I do know all about SQL, i.e. linking Perl to MySQL, but I really don't want to have to rely on a separate RDBMS.

        This SQLite thing looks pretty cool I will have a look at it. I'm not sure if I mentioned it before, but this is all part of my auction software. At the moment i'm storing everything in CSV files, it works fine, but i've been thinking about upgrading to a RDBMS like MySQL, but just haven't got around to it yet, perhaps i'll give SQLite a go instead.

        Cheers,
        Tom

        Ok i've looked at SQLite and even after looking at the man page, i'm not sure how to install it... i've downloaded the package and it'd full of foo.c and foo.h files. I'm not sure what they are all about I'm also working on Activestate Win32 Perl so makes things more complicated. Do I just need to put the SQLite.pm file in my lib/DBD folder?

        Ive tried using PPM (Perl Package Manager that comes with Activestate perl) but it can't find sqlite.

        Any ideas....?

        cheers,
        Tom

        UPDATE - Sorry just noticed the main site address so i'll check that out first! http://www.hwaci.com/sw/sqlite/