Dear community,
I have an xml file which was converted into a hash variable via XML::Simple.
use XML::Simple qw(:strict);
$config = XMLin('config.xml', KeyAttr => {item=>'filename'}, ForceArra
+y => ['item']);
My xml file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<item>
<id>0</id>
<filename>AMEX</filename>
<destination_dir>test</destination_dir>
</item>
<item>
<id>1</id>
<filename>bla</filename>
<destination_dir>test1</destination_dir>
</item>
<item>
<id>2</id>
<filename>alb</filename>
<destination_dir>test2</destination_dir>
</item>
</config>
the problem is, that I must have 2 possibilities to access the information in the <item>
The first case is to access a single item by its filename like:
print $config->{item}{"bla"}->{destination_dir};
It works fine!
The second case ist, to go through the whole config (item after item) and here I need to access each single item by an index like:
for (my $i = 0; $i < $theNumberOfItems; $i++)
{
print $config->{item}[$i]->{destination_dir};
}
But if I try this, I get an error:
Not an ARRAY reference at test.pl line 18.
I try to make the <id> to another key:
$config = XMLin('config.xml', KeyAttr => {item=>'filename', item=>'id'}, ForceArray => [ 'item']); and then to access like this:
for (my $i = 0; $i < $theNumberOfItems; $i++)
{
print $config->{item}{$i}->{destination_dir};
}
It workes fine, but now the access via filename is impossible. I suspect, that it is not allowed to have two of KeyAttr and the last one overwrites the filename...
Is there any possibility to access nevertheless on both ways?
Thanks a lot!
Dimitri
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.