Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Reading multi-level-tag XML file

by choroba (Cardinal)
on Jul 27, 2015 at 14:48 UTC ( [id://1136474]=note: print w/replies, xml ) Need Help??


in reply to Reading multi-level-tag XML file

Now you know why XML::Simple says
The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.

You can solve your issues by using the ForceArray option to XMLin:

my $data = XMLin($xml_string, ForceArray => 1); for my $inf (@{ $data->{SellerInformation} }) { print $inf->{Seller}[0]{sellerIdFromProvider}, "\n"; for my $location (@{ $inf->{TaxableLocationsCollection}[0]{Taxable +Location} }) { print "\t", $location->{locationValue}, "\n"; } }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Reading multi-level-tag XML file
by CSharma (Sexton) on Jul 27, 2015 at 16:28 UTC
    Thanks Choroba! That worked. I want output like below, but getting unexpected results. All location values of all tags are coming for all seller entries. Something here is messy, Could you please help? sellerid comma seperated locations i.e. 426089 NY, CA, FL
    for my $inf (@{$data->{'SellerInformation'}}) { $sellerid = $inf->{'Seller'}[0]{'sellerIdFromProvider'}; $i = 0; for my $loc (@{$inf->{'TaxableLocationsCollection'}[0]{'TaxableLocatio +n'}}) { $location[$i++] = $loc->{'locationValue'}; } $location{$sellerid} = join(", ", @location); print $sellerid, "\t" . $location{$sellerid} . "\n"; }

      You are not clearing the @location array for each Seller. Try

      for my $inf (@{$data->{'SellerInformation'}}) { my $sellerid = $inf->{'Seller'}[0]{'sellerIdFromProvider'}; my @location=(); for my $loc (@{$inf->{'TaxableLocationsCollection'}[0]{'TaxableLocat +ion'}}) { push @location,$loc->{'locationValue'}; } print $sellerid."\t" .join(", ", @location)."\n"; }
      poj
        Thanks poj, got the required data!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found