When I add an attribute to lets say tag B...

You are getting the error message because XML::Simple creates a data structure with an array for your elements but a simple scalar value for your attribute.

You can see this if you use Data::Dumper to dump $doc after loading your file.

After changing your B tag to <B type="xxx"> your script produced the following error: Can't use string ("xxx") as an ARRAY ref while "strict refs" in use. Here is the structure produced by XMLin:

$VAR1 = { 'Z' => [ { 'C' => [ { 'element' => [ { 'name' => 'z', 'type' => 'k', 'online' => 'yes' }, { 'name' => 'p', 'type' => 't', 'online' => 'yes' } ] } ], 'E' => [ { 'element' => [ { 'name' => 'pd', 'type' => 'kd', 'online' => 'no' }, { 'name' => 'ed', 'type' => 'id', 'online' => 'yes' } ] } ] } ], 'B' => [ { 'D' => [ { 'element' => [ { 'name' => 'pd', 'type' => 'kd', 'online' => 'no' }, { 'name' => 'ed', 'type' => 'id', 'online' => 'yes' }, { 'name' => 'zd', 'type' => 'yd', 'online' => 'no' } ] } ], 'C' => [ { 'element' => [ { 'name' => 'p', 'type' => 'k', 'online' => 'yes' }, { 'name' => 'e', 'type' => 'i', 'online' => 'yes' } ] } ], 'type' => 'xxx' } ] };

If you look carefully at what has been created for your tag B, you will see that there are three elements in the contained hash, with keys D, C and type. The values of the first two (D and C) are references to arrays but the value of the third is a string. Your program uses all three as if they were references to arrays. Because you have used use strict; (which is a good thing) perl stops when it gets to the string and reports the error.

You can either write your program to check whether a value is a reference or not, using ref or you can provide parameters to XML::Simple to make it load the elements and tags consistently. Which approach is better will depend on what you are doing. You may want to learn both methods so you can decide which is most appropriate.


In reply to Re^3: XML Attribute vs Element by ig
in thread XML Attribute vs Element by Sporti69

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.