I want to parse an XML file, consisting, among others:
<xml>
<t>
<name>object</name>
<t1 name="a">A</t1>
<t1 name="b">B</t1>
<t2 name="c">C</t2>
</t>
...
</xml>
So, when I parse it with XML::Simple:
my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml");
I get that while $data->{t}->{object}->{t1}->{a}->{content} is expectedly “A”, I cannot get the “C” with $data->{t}->{object}->{t2}->{c}->{content}, because there is only one instance of “t2”.
In my script, I don’t want to have any differences between those two cases – I wish to get “C” exactly in the same way I got “A”.
How can that be done? I tried searching for the answer, but I only got to setting ForceArray to 1, but that didn’t solve the problem.
Thank you!
Try to define in exact the forcearray, like this:
I believe this should work. If you are afraid that there might be instances of t which have only one instance of t1 as well, change it to
['t1', 't2'].