in reply to Re: XML::Simple parsing :-(
in thread XML::Simple parsing :-(

and here is the original xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- Incident report from the Georgia DOT's Navigator ITS. --> <!-- Visit us online at http://georgia-navigator.com/. --> <!-- Raw data available at http://georgia-navigator.com/data/. --> <!-- Copyright (c) 2002 Georgia DOT. All rights reserved. --> <incident-report timestamp="2003-06-19 15:56:03 EDT"> <incident id="GDOT-INC-252421" level="3" status="active"> <type id="8">Incident</type> <description>High impact, Roadway Damage, Pickens Co.</description +> <impact id="3">High</impact> <cleanup timestamp="2003-06-20 00:35:00 EDT">12:35 AM Tomorrow</cl +eanup> <lanes>All Lanes</lanes> <location> <type id="6">Arterial</type> <county id="227">Pickens</county> <coord projection="GA State Plane West" datum="NAD83" unit="mete +rs" x="670707" y="494686" /> <description>Southbound SR 53 AT MARBLE HILL(SINKHOLE -MP 25.3) +(PICKENS CO)</description> </location> </incident>

Replies are listed 'Best First'.
Re: Re: Re: XML::Simple parsing :-(
by chip (Curate) on Jun 19, 2003 at 22:28 UTC
    Please read the documentation on XML::Simple, hm? Save us all a lot of time. It's a good module if you know how to use it.

    According to that documentation, you probably want the ForceArray option:

        my $ref = $xs->XMLin($doc, ForceArray => [ 'incident' ]);

    PS: You don't need quotes on $doc when you're passing it to the subroutine.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Believe me I read the documentation before I post here. Its usually my last resort when I have to ask someone.
      Force array option doesnt do what I expected it to do. It just makes arrays out of the elements below the GDOT-INC-252421 portions of the tag. I have read the documentation and have used that option as well as the keeproot option just dont quite get why its not doing what I expected it to do.
        my $ref = $xs->XMLin($doc, ForceArray => [ 'incident' ]); works if the 'id' attribute is removed from the Incident start tag. Changing the name to 'iid' also works.

        Adding KeyAttr => [ ] to the options fixes this. From the doc for XML::Simple:
        Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. If you do not want folding on input or unfolding on output you must setting this option to an empty list to disable the feature. 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).