Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: XML::Simple help

by isotope (Deacon)
on Mar 20, 2008 at 16:46 UTC ( [id://675251]=note: print w/replies, xml ) Need Help??


in reply to XML::Simple help

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://675251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found