Config::Context::XMLSimple - Use XML-based config files with Config::Context
use Config::Context; my $config_text = ' <opt> <Location name="/users"> <title>User Area</title> </Location> <LocationMatch name="\.*(jpg|gif|png)$"> <image_file>1</image_file> </LocationMatch> </opt> '; my $conf = Config::Context->new( string => $config_text, driver => 'XMLSimple', match_sections => [ { name => 'Location', match_type => 'path', }, { name => 'LocationMatch', match_type => 'regex', }, ], ); my %config = $conf->context('/users/~mary/index.html'); use Data::Dumper; print Dumper(\%config); -------- $VAR1 = { 'title' => 'User Area', 'image_file' => undef, }; my %config = $conf->context('/users/~biff/images/flaming_logo.gif'); print Dumper(\%config); -------- $VAR1 = { 'title' => 'User Area', 'image_file' => 1, };
This module uses XML::Simple to parse XML config files for Config::Context. See the Config::Context docs for more information.
XML::Simple
Config::Context
By default, it is assumed that the RootName of your configuration files is <opt>. For instance:
RootName
<opt>
<opt> <Location /users> <title>Users Area</title> </Location> <opt>
If you change this to some other element, then you must specify the RootName parameter in driver_options:
driver_options
# Change the name of the root block to <Config>..</Config> my $conf = Config::Context->new( driver => 'XMLSimple', driver_options => { XMLSimple = > { RootName => 'Config', }, }, );
By default the options passed to XML::Simple are:
KeyAttr => [], ForceArray => \@section_names,
...where @section_names is a list of the sections as defined in match_sections. This makes for consistently formatted configurations that are similar to those generated by the other drivers.
match_sections
You can change this behaviour by passing a different value to driver_params to new:
driver_params
new
my $conf = Config::Context->new( driver => 'XMLSimple', driver_options => { XMLSimple = > { ForceArray => 1, }, }, );
You include XML files within other XML files by using the XInclude syntax. To include a file called other_config.xml you would use:
XInclude
other_config.xml
<opt> <xi:include href="other_config.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> </opt>
Files included this way are included in the same scope. For instance:
# config.xml <opt> <Location /users> <title>Users Area</title> </Location> <xi:include href="other_config.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <opt> # other_config.xml <opt> <Location /users> <title>Members Area</title> </Location> </opt>
In this example, the raw config will look like
{ 'location' => { 'users' => { 'title' => 'Members Area', } } }
And the config matching users will look like:
{ 'title' => 'Members Area', }
Note that the placement of the <xi:include> tag within a block (e.g. top or bottom) doesn't matter. Contents are merged into the block so that the included file has precedence.
<xi:include>
my $driver = Config::Context::XMLSimple->new( file => $config_file, options => { # ... } );
or:
my $driver = Config::Context::XMLSimple->new( string => $config_string, options => { # ... } );
Returns a new driver object, using the provided options.
Returns the data structure for the parsed config.
Returns a list of all the config files read, including any config files included in the main file.
Returns the modules required to parse the config. In this case: XML::Simple, XML::SAX and XML::Filter::XInclude.
XML::SAX
XML::Filter::XInclude
The lower_case_names option is not supported used with this driver. If you specify it, it will produce a warning.
lower_case_names
Config::Context CGI::Application::Plugin::Config::Context XML::Simple
Copyright 2004-2005 Michael Graham, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Config::Context, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Context
CPAN shell
perl -MCPAN -e shell install Config::Context
For more information on module installation, please visit the detailed CPAN module installation guide.