XML::Simple folding broken for single items?

Discussion in 'Perl Misc' started by it_says_BALLS_on_your_forehead, Feb 27, 2006.

  1. To: Grant McLean

    am i missing something? it appears that "folding" is broken for single
    items. this is for ver. 2.14.
    i'm basing subroutine on the data structure created by XMLin(), but it
    assumes that folding will occur even on single elements. did i miss
    something in the docs? i just would like to know oif this is working as
    it should, and i need to code around it, or if this is a bug. try the
    code below with $good versus $bad, and you will see what i mean. the
    $good string is the same as in the docs. thanks! LOVE the module btw
    :-D.

    ==================
    use strict; use warnings;

    use XML::Simple;
    use Data::Dumper;

    my $good = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    q{<user login="stty" fullname="Simon T Tyson" /></opt>};
    my $bad = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    q{</opt>};

    my $ref = XMLin($bad,
    #ForceArray => 1,
    KeyAttr => ['login'],
    GroupTags => {data => 'item'},
    );

    print Dumper($ref);

    =====

    $VAR1 = {
    'user' => {
    'fullname' => 'Gary R Epstein',
    'login' => 'grep'
    }
    };
     
    it_says_BALLS_on_your_forehead, Feb 27, 2006
    #1
    1. Advertising

  2. it_says_BALLS_on_your_forehead

    Matt Garrish Guest

    "it_says_BALLS_on_your_forehead" <> wrote in message
    news:...
    > To: Grant McLean
    >
    > am i missing something? it appears that "folding" is broken for single
    > items. this is for ver. 2.14.
    > i'm basing subroutine on the data structure created by XMLin(), but it
    > assumes that folding will occur even on single elements. did i miss
    > something in the docs? i just would like to know oif this is working as
    > it should, and i need to code around it, or if this is a bug. try the
    > code below with $good versus $bad, and you will see what i mean. the
    > $good string is the same as in the docs. thanks! LOVE the module btw
    > :-D.
    >
    > ==================
    > use strict; use warnings;
    >
    > use XML::Simple;
    > use Data::Dumper;
    >
    > my $good = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    > q{<user login="stty" fullname="Simon T Tyson" /></opt>};
    > my $bad = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    > q{</opt>};
    >
    > my $ref = XMLin($bad,
    > #ForceArray => 1,


    Why did you comment out that line? As per the documnetation for KeyAttr:

    <quote>
    Note 2: If you wish to use this option, you should also enable the
    ForceArray option. Without 'ForceArray', a single nested element will be
    rolled up into a scalar rather than an array and therefore will not be
    folded (since only arrays get folded).
    </quote>

    Setting ForceArray gives me exactly the same result as when there are two+
    elements, so I don't see any bug here.

    Matt
     
    Matt Garrish, Feb 27, 2006
    #2
    1. Advertising

  3. Matt Garrish wrote:
    > "it_says_BALLS_on_your_forehead" <> wrote in message
    > news:...
    > > To: Grant McLean
    > >
    > > am i missing something? it appears that "folding" is broken for single
    > > items. this is for ver. 2.14.
    > > i'm basing subroutine on the data structure created by XMLin(), but it
    > > assumes that folding will occur even on single elements. did i miss
    > > something in the docs? i just would like to know oif this is working as
    > > it should, and i need to code around it, or if this is a bug. try the
    > > code below with $good versus $bad, and you will see what i mean. the
    > > $good string is the same as in the docs. thanks! LOVE the module btw
    > > :-D.
    > >
    > > ==================
    > > use strict; use warnings;
    > >
    > > use XML::Simple;
    > > use Data::Dumper;
    > >
    > > my $good = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    > > q{<user login="stty" fullname="Simon T Tyson" /></opt>};
    > > my $bad = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    > > q{</opt>};
    > >
    > > my $ref = XMLin($bad,
    > > #ForceArray => 1,

    >
    > Why did you comment out that line? As per the documnetation for KeyAttr:
    >
    > <quote>
    > Note 2: If you wish to use this option, you should also enable the
    > ForceArray option. Without 'ForceArray', a single nested element will be
    > rolled up into a scalar rather than an array and therefore will not be
    > folded (since only arrays get folded).
    > </quote>
    >
    > Setting ForceArray gives me exactly the same result as when there are two+
    > elements, so I don't see any bug here.



    ahh, i missed that in the docs (going blind). i did notice that it
    worked if i turned ForceArray on, but I thought it should do it if
    ForceArray was off as well. The reason i turned it off was because it
    seemed to disable GroupTag (or maybe i just missed something else in
    the documentation, although i looked).

    ====
    use strict; use warnings;

    use XML::Simple;
    use Data::Dumper;
    $XML::Simple::pREFERRED_PARSER = 'XML::parser';

    my $line = q{<application_log name="BranchFinder"><event t="123">} .
    q{<data><item><name>zip</name><value>02109</value></item>} .
    q{<item><name>method</name><value>Get</value></item>} .
    q{<item><name>zip2</name><value>22222</value></item>} .
    q{</data></event></application_log>};


    # GroupTags eliminates extra level of indirection
    # <data> must not contain attributes or non-<item> elements
    my $ref = XMLin($line,
    ForceArray => 1,
    #KeyAttr => ['name'],
    GroupTags => {data => 'item'},
    );

    print Dumper($ref);

    __END__

    $VAR1 = {
    'name' => 'BranchFinder',
    'event' => [
    {
    'data' => [
    {
    'item' => [
    {
    'value' => [
    '02109'
    ],
    'name' => [
    'zip'
    ]
    },
    {
    'value' => [
    'Get'
    ],
    'name' => [
    'method'
    ]
    },
    {
    'value' => [
    '22222'
    ],
    'name' => [
    'zip2'
    ]
    }
    ]
    }
    ],
    't' => '123'
    }
    ]
    };

    ======
    whereas if i turn off ForceArray, GroupTag works...
    $VAR1 = {
    'name' => 'BranchFinder',
    'event' => {
    'data' => {
    'zip' => {
    'value' => '02109'
    },
    'zip2' => {
    'value' => '22222'
    },
    'method' => {
    'value' => 'Get'
    }
    },
    't' => '123'
    }
    };
     
    it_says_BALLS_on_your_forehead, Feb 27, 2006
    #3
  4. it_says_BALLS_on_your_forehead wrote:
    > Matt Garrish wrote:
    > > "it_says_BALLS_on_your_forehead" <> wrote in message
    > > news:...
    > > > To: Grant McLean
    > > >
    > > > am i missing something? it appears that "folding" is broken for single
    > > > items. this is for ver. 2.14.
    > > > i'm basing subroutine on the data structure created by XMLin(), but it
    > > > assumes that folding will occur even on single elements. did i miss
    > > > something in the docs? i just would like to know oif this is working as
    > > > it should, and i need to code around it, or if this is a bug. try the
    > > > code below with $good versus $bad, and you will see what i mean. the
    > > > $good string is the same as in the docs. thanks! LOVE the module btw
    > > > :-D.
    > > >
    > > > ==================
    > > > use strict; use warnings;
    > > >
    > > > use XML::Simple;
    > > > use Data::Dumper;
    > > >
    > > > my $good = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    > > > q{<user login="stty" fullname="Simon T Tyson" /></opt>};
    > > > my $bad = q{<opt><user login="grep" fullname="Gary R Epstein" />} .
    > > > q{</opt>};
    > > >
    > > > my $ref = XMLin($bad,
    > > > #ForceArray => 1,

    > >
    > > Why did you comment out that line? As per the documnetation for KeyAttr:
    > >
    > > <quote>
    > > Note 2: If you wish to use this option, you should also enable the
    > > ForceArray option. Without 'ForceArray', a single nested element will be
    > > rolled up into a scalar rather than an array and therefore will not be
    > > folded (since only arrays get folded).
    > > </quote>
    > >
    > > Setting ForceArray gives me exactly the same result as when there are two+
    > > elements, so I don't see any bug here.

    >
    >
    > ahh, i missed that in the docs (going blind). i did notice that it
    > worked if i turned ForceArray on, but I thought it should do it if
    > ForceArray was off as well. The reason i turned it off was because it
    > seemed to disable GroupTag (or maybe i just missed something else in
    > the documentation, although i looked).
    >
    > ====
    > use strict; use warnings;
    >
    > use XML::Simple;
    > use Data::Dumper;
    > $XML::Simple::pREFERRED_PARSER = 'XML::parser';
    >
    > my $line = q{<application_log name="BranchFinder"><event t="123">} .
    > q{<data><item><name>zip</name><value>02109</value></item>} .
    > q{<item><name>method</name><value>Get</value></item>} .
    > q{<item><name>zip2</name><value>22222</value></item>} .
    > q{</data></event></application_log>};
    >
    >
    > # GroupTags eliminates extra level of indirection
    > # <data> must not contain attributes or non-<item> elements
    > my $ref = XMLin($line,
    > ForceArray => 1,
    > #KeyAttr => ['name'],
    > GroupTags => {data => 'item'},
    > );
    >
    > print Dumper($ref);
    >
    > __END__
    >
    > $VAR1 = {
    > 'name' => 'BranchFinder',
    > 'event' => [
    > {
    > 'data' => [
    > {
    > 'item' => [
    > {
    > 'value' => [
    > '02109'
    > ],
    > 'name' => [
    > 'zip'
    > ]
    > },
    > {
    > 'value' => [
    > 'Get'
    > ],
    > 'name' => [
    > 'method'
    > ]
    > },
    > {
    > 'value' => [
    > '22222'
    > ],
    > 'name' => [
    > 'zip2'
    > ]
    > }
    > ]
    > }
    > ],
    > 't' => '123'
    > }
    > ]
    > };
    >
    > ======
    > whereas if i turn off ForceArray, GroupTag works...
    > $VAR1 = {
    > 'name' => 'BranchFinder',
    > 'event' => {
    > 'data' => {
    > 'zip' => {
    > 'value' => '02109'
    > },
    > 'zip2' => {
    > 'value' => '22222'
    > },
    > 'method' => {
    > 'value' => 'Get'
    > }
    > },
    > 't' => '123'
    > }
    > };


    ....maybe because with ForceArray ON, 'item' maps to a ref of an array
    that contains 3 elements--hashrefs, while if ForceArray is OFF, 'item'
    maps to a ref of a hash with 3 key/value pairs? i still don't see why
    GroupTags would work with ForceArray OFF and not ON. the docs say "The
    grouping element must not contain any attributes or elements other than
    the grouped element." does this mean nested nested elements? if so,
    then GroupTags should not work for ForceArray => 0 since there are
    elements within <item>. if not, then GroupTag should work with
    ForceArray => 1.

    maybe i'm just too tired...
     
    it_says_BALLS_on_your_forehead, Feb 27, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GlennH
    Replies:
    3
    Views:
    693
    Martin Honnen
    Jun 22, 2004
  2. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    694
  3. Cameron Simpson

    Re: Why are "broken iterators" broken?

    Cameron Simpson, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    615
    Cameron Simpson
    Sep 22, 2008
  4. Fredrik Lundh

    Re: Why are "broken iterators" broken?

    Fredrik Lundh, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    628
    Fredrik Lundh
    Sep 22, 2008
  5. ela
    Replies:
    12
    Views:
    381
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page