Sidd has asked for the wisdom of the Perl Monks concerning the following question:

I have to parse the following XML and retrieve the value of text within <Name> tag.Can this be done using XML::Simple?
<?xml version="1.0" encoding="utf-8" ?> <Config type="Tabs"> <Core active="true"> <CategoryID>a</CategoryID> </Core> <ShoeBank> <default>1</default> <Shoe> <Name>a</Name> <Image>a.jpg</Image> <MainClip>a.swf</MainClip> <ShoeConfigXML>a.xml</ShoeConfigXML> </Shoe> <Shoe> <Name>b</Name> <Image>b.jpg</Image> <MainClip>b.swf</MainClip> <ShoeConfigXML>b.xml</ShoeConfigXML> </Shoe> </ShoeBank> </Config>

Replies are listed 'Best First'.
Re: Problem with parsing an XML using XML::Simple
by davido (Cardinal) on Aug 18, 2011 at 08:01 UTC

    Use Data::Dumper to dump the datastructure provided by XML::Simple's XMLin() function. That will tell you almost everything you need to know about where to find the data you're looking for.

    I say almost, because the datastructure will take on a different form at the slightest whiff of change to your incoming XML. For that reason, I prefer a more robust parser such as XML::LibXML, or XML::Twig. But if your XML is unlikely to change much (for example, ShoeBank always has more than one set of shoe(s), XML::Simple is enticing for its ease of use.


    Dave

      You can also use the perl debugger to examine the data structure produced by XMLin().

      The problem of the data structure changing if the incoming XML changes can mostly be fixed by passing the ForceArray option to XMLin() so that the nesting level of parameters you are interested in does not change when the number of nodes above changes.

Re: Problem with parsing an XML using XML::Simple
by tinita (Parson) on Aug 18, 2011 at 10:27 UTC
    why did you post this twice?
    I considered this node for removal because your first thread got already answers.
    now you have answers in two threads and my consideration is pointless.
Re: Problem with parsing an XML using XML::Simple
by Nikhil Jain (Monk) on Aug 18, 2011 at 08:57 UTC

    Yes you can do it by XML::Simple, take a look below

    use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = q~<?xml version="1.0" encoding="utf-8" ?> <Config type="Tabs"> <Core active="true"> <CategoryID>a</CategoryID> </Core> <ShoeBank> <default>1</default> <Shoe> <Name>a</Name> <Image>a.jpg</Image> <MainClip>a.swf</MainClip> <ShoeConfigXML>a.xml</ShoeConfigXML> </Shoe> <Shoe> <Name>b</Name> <Image>b.jpg</Image> <MainClip>b.swf</MainClip> <ShoeConfigXML>b.xml</ShoeConfigXML> </Shoe> </ShoeBank> </Config>~; print $xml,$/; my $data = XMLin($xml); print Dumper( $data ); foreach my $test (@{$data->{ShoeBank}{Shoe}}){ print"Name:$test->{Name} \n"; }

    Output: Name:a
    Name:b

      Thanks for replying Nikhil. I had tried something similar earlier but it somehow doesn't work for me. Following is the code. Even the dumper shows nothing. The XML is stored in the file home_flash.xml
      my $debug_file = "F:/Interwoven/OpenDeployNG/solutions/perl/comm/a +bc/home_edit_glblcnt.log"; open(OUT, ">$debug_file"); my $xml = XML::Simple->new(); my $shoeConfig = $xml->XMLin('f:\\inetpub\\ecomm-static\\us\\html\ +\mi_home_page\\home_flash.xml'); print OUT Dumper($shoeConfig); foreach my $shoe(@{$shoeConfig->{ShoeBank}{Shoe}}){ my $img = $shoe->{Image}; my $swf = $shoe->{MainClip}; print OUT "\img str - ".$img; print OUT "\swf str - ".$swf; }