in reply to Apache::ConfigFile iterate problem

Here's my understanding of what your situation. First, you have a complex data structure ("Hash of Hashes of (Hashes/Arrays)"). Second, you want to put this complex data structure into an XML file. Is that correct?

Assuming that my description above is correct, one method would be to use XML::Simple to generate the XML for you. The code for doing that would be something like this:

use strict; use XML::Simple; my $data; # insert code to create complex data structure in $data; my $xml = XMLout($data, XMLDecl => 1); open(FH,">output.xml") || die "Unable to open 'output.xml': $!\n"; print FH $xml; close FH;

If you don't care for XML::Simple, there are other modules that might be able to do something similar. I mentioned this module because I've used it and it sounded like you already had a data structure that could be used as is by this module.

Replies are listed 'Best First'.
Re^2: Apache::ConfigFile iterate problem
by tpais (Novice) on Sep 07, 2010 at 05:43 UTC

    Thank you for your code. This works wonderful for an singular vhost.

    (XML::Simple make an stop after one vhost. I don't know why.)

    I have multiple vhosts in multiple files and now I have a code which can become analyzed:

    foreach for $xml and open/close does not work well.

    use Apache::ConfigFile; foreach my $vh_file (@vhost_files) { next if $vh_file =~ /000.*/; my $ac = Apache::ConfigFile->read(file=>"$vhost_path/$vh_file", ig +nore_case=>0, fix_booleans=>0); my @vhkeys = $ac->cmd_context('VirtualHost'); for my $vhkey (@vhkeys) { foreach my $vhost ( $ac->cmd_context(VirtualHost => $vhkey) ) { my $sn=$vhost->cmd_config('ServerName'); $vh{$sn} = $vhost->data; } } } my $xml = XMLout(\%vh, XMLDecl => 1); open(FH,">>/tmp/output1.xml") || die "Unable to open 'output.xml': + $!\n"; print FH $xml; close FH;