Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML::Simple

Status
Not open for further replies.

pete1970

Programmer
Aug 16, 2007
10
US
This is most of an example from perl-xml-quickstart:

Code:
<?xml version="1.0"?>
<camelids>
  <species name="Camelus bactrianus">
    <common-name>Bactrian Camel</common-name>
    <physical-characteristics>
      <mass>450 to 500 kg.</mass>
      <appearance>
        The most noticeable feature of C. bactrianus is its two humps.
      </appearance>
    </physical-characteristics>
    <natural-history>
       <food-habits>
         Camels are herbivores.
       </food-habits>
       <reproduction>
         Mating season occurs in the fall, with inbreeding often involved in domesticated camels.
       </reproduction>
       <behavior>
         Domestic camels travel in caravans across the desert.
       </behavior>
       <habitat>
         The camel's habitat consists mainly of Asia's deserts.
       </habitat>
    </natural-history>
    <conservation status="endangered">
      <detail>
        Camels were thought to be extinct in the wild.
      </detail>
    </conservation>
  </species>
  <species name="Camelus dromedarius">
    <common-name>Dromedary, or Arabian Camel</common-name>
    <physical-characteristics>
      <mass>300 to 690 kg.</mass>
      <appearance>
        The dromedary camel is characterized by a long-curved neck, deep-narrow chest, and a single hump.
      </appearance>
    </physical-characteristics>
    <natural-history>
       <food-habits>
         The dromedary camel is a herbivore.
       </food-habits>
       <reproduction>
         The dromedary camel has a lifespan of about 40-50 years.
       </reproduction>
       <behavior>
         With the exception of rutting males, dromedaries show very little aggressive behavior.
       </behavior>
       <habitat>
         The camels prefer desert conditions characterized by a long dry season and a short rainy season.
       </habitat>
    </natural-history>
    <conservation status="no special status">
      <detail>
        Since the dromedary camel is domesticated, the camel has no special status in conservation (Busch Gardens 1996). 
      </detail>
    </conservation>
  </species>
  <species name="Lama glama">
    <common-name>Llama</common-name>
    <physical-characteristics>
      <mass>130 to 155 kg.</mass>
      <appearance>
        Llamas feet are slender and their limbs are long.
      </appearance>
    </physical-characteristics>
    <natural-history>
       <food-habits>
         Llamas are herbivorous, feeding on many kinds of grasses and leaves.
       </food-habits>
       <reproduction>
         Llamas reach sexual maturity at about 12-24 months.
       </reproduction>
       <behavior>
         The Lama glama lives only in domestication.
       </behavior>
       <habitat>
         Llamas are found in deserts, mountainous areas, and grasslands.
       </habitat>
    </natural-history>
    <conservation status="no special status">
      <detail>
        The population of llamas has declined since road building reduced their importance in transportation.
      </detail>
    </conservation>
  </species>
  <species name="Lama guanicoe">
    <common-name>Guanaco</common-name>
    <physical-characteristics>
      <mass>115 to 140 kg.</mass>
      <appearance>
        They stand at 1,100 to 1,200mm at the shoulder and have slender bodies with long limbs and neck.
      </appearance>
    </physical-characteristics>
    <natural-history>
       <food-habits>
         Guanacos are herbivores that can inhabit dry areas and forego drinking for long periods.
       </food-habits>
       <reproduction>
         Females are apparently induced ovulators.
       </reproduction>
       <behavior>
         There are three types of social groups: family groups, male troops, and solitary males.
       </behavior>
       <habitat>
         Guanacos inhabit grasslands and shrublands from sea level to 4,000m. Occasionally they winter in forests.
       </habitat>
    </natural-history>
    <conservation status="special concern">
      <detail>
        Guanacos have had their numbers drastically reduced due to human pressures of habitat encroachment, habitat
        destruction, and hunting.
      </detail>
    </conservation>
  </species>
  <species name="Vicugna vicugna">
    <common-name>Vicuna</common-name>
    <physical-characteristics>
      <mass>35 to 65 kg.</mass>
      <appearance>
        The vicuna is the smallest living species among the family Camelidae.
      </appearance>
    </physical-characteristics>
    <natural-history>
      <food-habits>
        The vicuna is strictly a grazer.
      </food-habits>
      <reproduction>
        Mating begins in March and April.
      </reproduction>
      <behavior>
        Vicunas are alert and shy animals that flee very rapidly.
      </behavior>
      <habitat>
        Vicunas are found in semiarid rolling grasslands and plains at altitudes of 3,500-5,750 meters.
      </habitat>
    </natural-history>
    <conservation status="endangered">
      <detail>
        The vicuna is classified as vulnerable by the IUCN, and as endangered by the USDI.
      </detail>
    </conservation>
  </species>
</camelids>

Some code to pull data from the XML:

Code:
#!/usr/bin/perl use strict;
use XML::Simple;
my $file = 'files/camelids.xml';
my $xs1 = XML::Simple->new();
my $doc = $xs1->XMLin($file, forcearray => 1);

foreach my $key (keys (%{$doc->{species}})){
   print $doc->{species}->{$key}->{'common-name'}[0];
   print " ($key) "; print "\n"; }

I can get data from the species key and the common-name (one level in) but how do you get, say, the physical-characteristics->mass? Things like:

Code:
print $doc->{species}->{$key}->{'physical-characteristics'}[0];

and

Code:
print $doc->{species}->{$key}->{'physical-characteristics'}['mass'];

only give the hashref. Do I need another foreach loop? Thanks
 
When dealing with complex data structures, it helps sometimes to be able to visually see the structure.

Code:
use Data::Dumper;
print Dumper($doc);

That might print something along the lines of...

Code:
$VAR1 = {
   species => {
      'Camelus bactrianus' => {
         common_name                => 'Bactrian Camel',
         'physical-characteristics' => {
            mass      => '450 to 500 kg.',
            apperance => 'The most noticeable feature of C. bactrianus is its two humps.',
         },
         'natural-history' => {
            # and so-on...
         },
      },
   },
   # ...
};

And then all the {} braces are hash references, and the [] are array references. So then you'd be able to see how the structure works for use in your code.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
I have done that but am still confused on the syntax of how to access the deeper data.
 
I used:

Code:
print $doc->{species}{$key}->{'physical-characteristics'}[0]->{mass}[0];

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top