M
Mark
Hi,
As a newbie, I'm trying to use XML::Simple and I'm not very
successful... So please help
Consider the following XML Schema:
<class name="SIMPLE">
<export>
<object class="Collection" name="cFIRST">
<Integer name="f1" constant="true">1</Integer>
<Integer name="f2" constant="true">2</Integer>
</object>
<object class="Collection" name="cSECOND">
<Integer name="aaa" constant="true">0</Integer>
<Integer name="bbb" constant="true">1</Integer>
</object>
<String name="myString" length="10"></String>
<Integer name="myInt">1</Integer>
</export>
</class>
I want to fill a list with all the elements under tag <export> as
following (as strings):
"cFIRST.f1"
"cFIRST.f2"
"cSECOND.aaa"
"cSECOND.bbb"
"myString"
"myInt"
I've written the following code but my list is always empty...
use XML::Simple;
use strict;
use warnings;
my $xml_in = $ARGV[0];
my $content = eval { XMLin($xml_in) };
print("I found an error in your XML: $@") if $@;
my @list = ();
my $nb;
foreach my $obj (keys %{$content->{class}->{export}->{object}}) {
my @obj = keys %{$content->{class}->{export}->{$obj}->{Integer}};
$nb = @obj;
print "For $obj there are $nb items\n" ;
foreach my $item (@obj) {
push (@list, "$obj.$item" )
};
}
$nb = @list;
printf "\nThere are $nb items in the list\n";
foreach my $i (@list) {
printf "$i\n";
}
I guess my problem is here : "->{$obj}->", but I still can't find how
to resolve it.
Thanks in advance for any help.
Any advice for optimization is also welcome.
Mark
As a newbie, I'm trying to use XML::Simple and I'm not very
successful... So please help
Consider the following XML Schema:
<class name="SIMPLE">
<export>
<object class="Collection" name="cFIRST">
<Integer name="f1" constant="true">1</Integer>
<Integer name="f2" constant="true">2</Integer>
</object>
<object class="Collection" name="cSECOND">
<Integer name="aaa" constant="true">0</Integer>
<Integer name="bbb" constant="true">1</Integer>
</object>
<String name="myString" length="10"></String>
<Integer name="myInt">1</Integer>
</export>
</class>
I want to fill a list with all the elements under tag <export> as
following (as strings):
"cFIRST.f1"
"cFIRST.f2"
"cSECOND.aaa"
"cSECOND.bbb"
"myString"
"myInt"
I've written the following code but my list is always empty...
use XML::Simple;
use strict;
use warnings;
my $xml_in = $ARGV[0];
my $content = eval { XMLin($xml_in) };
print("I found an error in your XML: $@") if $@;
my @list = ();
my $nb;
foreach my $obj (keys %{$content->{class}->{export}->{object}}) {
my @obj = keys %{$content->{class}->{export}->{$obj}->{Integer}};
$nb = @obj;
print "For $obj there are $nb items\n" ;
foreach my $item (@obj) {
push (@list, "$obj.$item" )
};
}
$nb = @list;
printf "\nThere are $nb items in the list\n";
foreach my $i (@list) {
printf "$i\n";
}
I guess my problem is here : "->{$obj}->", but I still can't find how
to resolve it.
Thanks in advance for any help.
Any advice for optimization is also welcome.
Mark