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

I'm trying to provide a tool that will allow a user to create their own control menu. The menu file format is in XML. In my design, there are 3 types of menu "items" that can be defined. The item's attributes are determined by the item type (command, submenu, separator) The command that I'm using to read the xml:
use XML::Simple qw(:strict); XMLin($menufile,forcearray=>['item','command','exec','menu'],keyattr=> +['name'],suppressempty=>undef);
I expected that all "item"s would be turned into arrays, but that doesn't happen when trying to build the "SoftwareMenu" which only contains other submenus. I get a "Not an ARRAY reference" error. If I add a "blank" item to the SoftwareMenu, the code works. Any help would be appreciated. An example of my XML:
<opt> <menu name="MainMenu"> <label> OPS Menu </label> <item type="command"> <label> EDM </label> <exec> <run> edmBeta </run> </exec> </item> <item type="submenu"> <name> SoftwareMenu </name> </item> </menu> <menu name="SoftwareMenu"> <label> Software Tools </label> <item type="submenu"> <name> IOCMenu </name> </item> </menu> </opt>

Replies are listed 'Best First'.
Re: XML::Simple help
by isotope (Deacon) on Mar 20, 2008 at 16:46 UTC
    XML::Simple doesn't know your schema ahead of time; it only knows what it reads. If there's no item element under your menu, then it doesn't know that there could be. Your options:
    1. Test for existence of an item element, its defined-ness, and whether it's an arrayref before trying to use it as one
    2. Make sure to always put at least an empty item element (not so useful if you're letting a user make their own file)
    3. Use a different (schema-aware) XML parser that can know ahead of time what you expect.

    Update: Ah, I see I've misunderstood your question. For future reference, could you use Data::Dumper to make your explanation a lot more clear? In my words, the problem you have is that the single item element in your Software Menu is not being forced into an array, despite the ForceArray setting in XMLin(). I don't know the answer to that question.

    Update 2: I believe it's your setting for keyattr that's giving you trouble. If you change it to keyattr => { menu => 'name' }, you will get an arrayref in SoftwareMenu->{item}.

    Note that you'll have to do more legwork to get the item's name attribute folded, as adding it to keyattr (item => 'name') will require that you either disable XML::Simple's STRICT MODE, or ensure every item has a name attribute:
           Note: "XMLin()" will generate a warning (or a fatal error in "STRICT
           MODE") if this syntax is used and an element which does not have the
           specified key attribute is encountered (eg: a 'package' element without
           an 'id' attribute, to use the example above).  Warnings will only be
           generated if -w is in force.
    


    --isotope
Re: XML::Simple help
by toolic (Bishop) on Mar 20, 2008 at 17:23 UTC
    Try this:
    #!/usr/bin/env perl use warnings; use strict; use Data::Dumper; my $menufile = './in.xml'; use XML::Simple qw(:strict); my $xml = XMLin($menufile,forcearray=>['item','command','exec','menu'] +,keyattr=>[],suppressempty=>undef); print Dumper($xml);

    Which prints out (i.e., all "items" are arrays):

    The difference is keyattr=>[].

    I honestly am not very experienced with XML::Simple, but I followed a link from the docs to XML/Simple/FAQ where it discusses "array folding".