XML, the Perl Way

Processing XML with Perl Michel Rodriguez

Introduction to XML::Simple

XML::DOM

Example: loading a configuration file

A configuration file:

<config logdir="/var/log/foo/" debugfile="/tmp/foo.debug">
  <server name="sahara" osname="solaris" osversion="2.6">
    <address>10.0.0.101</address>
    <address>10.0.1.101</address>
  </server>
  <server name="gobi" osname="irix" osversion="6.5">
    <address>10.0.0.102</address>
  </server>
  <server name="kalahari" osname="linux" osversion="2.0.34">
    <address>10.0.0.103</address>
    <address>10.0.1.103</address>
  </server>
</config>

A simple script:

  use XML::Simple;
  my $config = XMLin();                                # load the file
  print $config->{logdir};                             # the log dir
  print $config->{server}->{kalahari}->{address}->[1]; # the second address on the server 'kalahari'
                                                       # (name is a key attribute) 
  $config->{debugfile}= "/dev/null";                   # change the debug file
  XMLout();                                            # output the updated XML file


Introduction to XML::Simple

XML::DOM