Frank John has asked for the wisdom of the Perl Monks concerning the following question:

use strict ; use warnings ; use XML::Simple ; my $xml = <<'EOD' ; <database name="somedb"> <table name="table1"> <column name="column11" type="CHAR" /> <column name="column12" type="CHAR" /> </table> <table name="table2"> <column name="column21" type="CHAR" /> <column name="column22" type="CHAR" /> </table> </database> EOD my $data = XMLin( $xml, forcearray => 1 ) ; foreach my $key ( keys %{$data->{'table'}} ) { print "$key \n"; #foreach my $column ( @{$data->{'table'}{$key}{'column'}{name}} ) # { # print "key = $key; column = $column\n" ; # } }
I can print out the table name, but could not print out the column name in each table.

Anyone could help?

2005-01-04 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Help!! XML::Simple with nested arrays
by ikegami (Patriarch) on Jan 04, 2005 at 17:53 UTC

    Try using Data::Dumper to examine the structure of $data. Then you might have better luck at figuring out how to access the various fields.

    use Data::Dumper; print(Dumper($data));

    You need

    use strict ; use warnings ; use XML::Simple ; my $xml = <<'EOD' ; <database name="somedb"> <table name="table1"> <column name="column11" type="CHAR" /> <column name="column12" type="CHAR" /> </table> <table name="table2"> <column name="column21" type="CHAR" /> <column name="column22" type="CHAR" /> </table> </database> EOD my $data = XMLin( $xml, forcearray => 1 ) ; foreach my $table_name ( keys(%{$data->{'table'}}) ) { my $table = $data->{'table'}{$table_name}; foreach my $column_name ( keys(%{$table->{'column'}}) ) { my $column = $table->{'column'}{$column_name}; my $column_type = $column->{'type'}; print("Type of column $column_name in table $table_name is $colu +mn_type.\n"); } }
      Below is a running program --- I can print out the column in a table. Please tell me how to print out "javaname" and "type" attribute. Thanks in advance!
      my $xml = <<'EOD' ; <database name="somedb"> <table name="table1"> <column name="column11" javaname="jn11" type="CHAR" /> <column name="column12" javaname="jn12" type="CHAR" /> </table> <table name="table2"> <column name="column21" javaname="jn21" type="CHAR" /> <column name="column22" javaname="jn22" type="CHAR" /> </table> </database> EOD my $data = XMLin( $xml, forcearray => 1 ) ; foreach my $key ( keys %{$data->{'table'}} ) { foreach my $column ( keys %{$data->{'table'}->{$key}->{column}} ) { print "table = $key; column = $column\n" ; } }

      20050104 Edit by ysth: code tags

        When you post code (such as the XML and the Perl code above), please wrap it in <code> tags to make it readable.

        use strict ; use warnings ; use XML::Simple ; my $xml = <<'EOD' ; <database name="somedb"> <table name="table1"> <column name="column11" javaname="jn11" type="CHAR" /> <column name="column12" javaname="jn12" type="CHAR" /> </table> <table name="table2"> <column name="column21" javaname="jn21" type="CHAR" /> <column name="column22" javaname="jn22" type="CHAR" /> </table> </database> EOD my $data = XMLin( $xml, forcearray => 1 ) ; foreach my $table_name ( keys(%{$data->{'table'}}) ) { my $table = $data->{'table'}{$table_name}; foreach my $column_name ( keys(%{$table->{'column'}}) ) { my $column = $table->{'column'}{$column_name}; my $column_type = $column->{'type'}; my $column_java = $column->{'javaname'}; print("Type of column $column_name in table $table_name is +$column_type.\n"); print("Java name of column $column_name in table $table_name is +$column_java.\n"); } }
Re: Help!! XML::Simple with nested arrays
by davido (Cardinal) on Jan 04, 2005 at 18:31 UTC

    There is a problem with the following line:

    my $data = XMLin( $xml, forcearray => 1 ) ;

    Per the documentation for XML::Simple, 'forcearray' should be spelled 'ForceArray'. This doesn't seem to be a problem for your script, but could potentially be a problem down the road.


    Dave

      Older versions of XML::Simple (such as the one that came with ActivePerl 5.6.1) required lowercase option names. Newer versions are case-insensitive. Using "forcearray" is backwards compatible, whereas "ForceArray" is not.

        Interesting. ...the docs simply mention ForceArray, so the documentation is basically teaching a usage that is not backward compatible. Good to know about the case insensitivity though. I didn't use XML::Simple back in the Perl 5.6.x days. Thanks for the enlightenment.


        Dave

Re: Help!! XML::Simple with nested arrays
by borisz (Canon) on Jan 04, 2005 at 17:53 UTC
    Sure,
    use XML::Simple; my $xml = <<'EOD' ; <database name="somedb"> <column name="column11" type="CHAR" /> <column name="column12" type="CHAR" /> <column name="column21" type="CHAR" /> <column name="column22" type="CHAR" /> </database> EOD my $data = XMLin( $xml, forcearray => 1 ); print $data->{name}, $/; for my $col ( keys %{ $data->{column} } ) { print $col, $/; }
    Boris
      Your XML doesn't match the OP's, so your code doesn't work. You probably got confused by the initial lack of <code> tags in the OP.
        No, I copied the XML right from the op's post. The OP changed the XML!.
        Boris