Search code examples
xmlperlperl-data-structures

What is the best way to write and read XML in perl?


I am working with Perl programs that write to an XML file by just using the typical functions open, print, close. The XML files are later digested by a PHP Web application.

#!/usr/bin/perl
#opening file
open FILE, ">derp.xml" or die $!;

#data is created in variables like so...
$first       = '<\?xml version=\"1.0\" encoding=\"UTF-8\" \?>';
$openperson  = '<person>\n';
$name        = '<name>Gary</name>\n';
$birthday    = '<brithday>01/10/1999</birthday>\n';
$car         = '<car>minivan</car>\n';
$closeperson = '</person>\n';

#writing variables to file
print FILE $first;
print FILE $openperson;
print FILE $name;
print FILE $birthday;
print FILE $car;
print FILE $closeperson;
close FILE;

More or less this is basically how the current system works. I am sure there must be a better way.


Solution

  • What's about these CPAN modules:

    • XML::LibXML
    • XML::Writer
    • XML::Simple