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

I have an XML File

<response> <version>0.1</version> <termsofService>http://www.medline.com/</termsofService> <features> <feature>history</feature> </features> <history> <observations> <observation> <Date PubStatus="entrez"> <Year>2010</Year> <Month>6</Month> <Day>18</Day> <Hour>6</Hour> <Minute>0</Minute> </Date> <PublicationStatus>aheadofprint</PublicationStatus> <Initials>JJ</Initials> <NlmUniqueID>8434563</NlmUniqueID> </observation> <observation> <Date PubStatus="entrez"> <Year>2011</Year> <Month>4</Month> <Day>18</Day> <Hour>10</Hour> <Minute>20</Minute> </Date> <PublicationStatus>aheadofprint</PublicationStatus> <Initials>BP</Initials> <NlmUniqueID>9814863</NlmUniqueID> </observation> </observations> </history> </response>

I want to extract everything under the observation tag. I was able to parse a simple XML file using XML::Simple and only one observation tag and get the output but I am not able to extract information off of repetitive multi level tags containing repetitive tags. Please help me figure it out. Thanks, Gouri

  • Comment on Extracting from repetitive multi level tags containing repetitive tags using Perl
  • Download Code

Replies are listed 'Best First'.
Re: Extracting from repetitive multi level tags containing repetitive tags using Perl
by ww (Archbishop) on Jan 20, 2012 at 20:16 UTC
    The problem is probably at line 42.

    Unless, in addition to all our other talents and attributes, you expect us to be mind-readers, you'll edit your question to show us what you tried, and how it failed.

Re: Extracting from repetitive multi level tags containing repetitive tags using Perl
by Gouri (Initiate) on Jan 20, 2012 at 21:34 UTC

    Hi Guys, I figured it out.. I had been working on this for over a while but I figured my solution out..

    nevertheless, I'll show you what I did, I did put in some modifications so that my data is displayed as needed by me

    The file I pasted was a sample file, my actual file looks like this..

    <response> <version>0.1</version> <termsofService>http://www.wunderground.com/weather/api/d/terms.ht +ml</termsofService> <features> <feature>history</feature> </features> <history> <date> <pretty>October 4, 2010</pretty> <year>2010</year> <mon>10</mon> <mday>04</mday> <hour>12</hour> <min>00</min> <tzname>America/New_York</tzname> </date> <utcdate> <pretty>October 4, 2010</pretty> <year>2010</year> <mon>10</mon> <mday>04</mday> <hour>16</hour> <min>00</min> <tzname>UTC</tzname> </utcdate> <observations> <observation> <date> <pretty>12:54 AM EDT on October 04, 2010</pret +ty> <year>2010</year> <mon>10</mon> <mday>04</mday> <hour>00</hour> <min>54</min> <tzname>America/New_York</tzname> </date> <utcdate> <pretty>4:54 AM GMT on October 04, 2010</prett +y> <year>2010</year> <mon>10</mon> <mday>04</mday> <hour>04</hour> <min>54</min> <tzname>UTC</tzname> </utcdate> <tempm>13.9</tempm> <tempi>57.0</tempi> <dewptm>7.8</dewptm> <dewpti>46.0</dewpti> <hum>67</hum> <wspdm>35.2</wspdm> <wspdi>21.9</wspdi> <wgustm>48.2</wgustm> <wgusti>29.9</wgusti> <wdird>40</wdird> <wdire>NE</wdire> <vism>16.1</vism> <visi>10.0</visi> <pressurem>1022.5</pressurem> <pressurei>30.20</pressurei> <windchillm>-999</windchillm> <windchilli>-999</windchilli> <heatindexm>-9999</heatindexm> <heatindexi>-9999</heatindexi> <precipm>-9999.00</precipm> <precipi>-9999.00</precipi> <conds>Overcast</conds> <icon>cloudy</icon> <fog>0</fog> <rain>0</rain> <snow>0</snow> <hail>0</hail> <thunder>0</thunder> <tornado>0</tornado> <metar>METAR KBOS 040454Z 04019G26KT 10SM SCT031 B +KN110 OVC140 14/08 A3020 RMK AO2 PK WND 05027/0439 SLP225 T01390078 4 +01500083</metar> </observation> <observation> <date> <pretty>1:54 AM EDT on October 04, 2010</prett +y> <year>2010</year> <mon>10</mon> <mday>04</mday> <hour>01</hour> <min>54</min> <tzname>America/New_York</tzname> </date> <utcdate> <pretty>5:54 AM GMT on October 04, 2010</prett +y> <year>2010</year> <mon>10</mon> <mday>04</mday> <hour>05</hour> <min>54</min> <tzname>UTC</tzname> </utcdate> <tempm>12.8</tempm> <tempi>55.0</tempi> <dewptm>8.3</dewptm> <dewpti>46.9</dewpti> <hum>74</hum> <wspdm>29.6</wspdm> <wspdi>18.4</wspdi> <wgustm>42.6</wgustm> <wgusti>26.5</wgusti> <wdird>50</wdird> <wdire>NE</wdire> <vism>16.1</vism> <visi>10.0</visi> <pressurem>1023.0</pressurem> <pressurei>30.21</pressurei> <windchillm>-999</windchillm> <windchilli>-999</windchilli> <heatindexm>-9999</heatindexm> <heatindexi>-9999</heatindexi> <precipm>0.0</precipm> <precipi>0.00</precipi> <conds>Light Rain</conds> <icon>rain</icon> <fog>0</fog> <rain>1</rain> <snow>0</snow> <hail>0</hail> <thunder>0</thunder> <tornado>0</tornado> <metar>METAR KBOS 040554Z 05016G23KT 10SM -RA FEW0 +18 OVC095 13/08 A3021 RMK AO2 PK WND 04027/0458 RAB18 SLP230 P0000 60 +000 T01280083 10139 20128 55000</metar> </observation> </observations> </history> </response>

    here is what i am trying to do..

    #!usr/bin/perl #use strict; use warnings; use XML::TreeBuilder; #use XML::Parser; #use Data::Dumper; my $root= XML::TreeBuilder->new(); $root->parsefile('weateg.XML'); #print Dumper($root); my @observation=$root->look_down(_tag=>'date'); foreach my $h (@observation) { printf "%s: %d-%d-%d %d:%d\t", $h->attr('date'), $h->look_down(_tag => 'year')->as_text,$h->look_down(_tag => 'mon' +)->as_text,$h->look_down(_tag => 'mday')->as_text,$h->look_down(_tag +=> 'hour')->as_text,$h->look_down(_tag => 'min')->as_text; }
A reply falls below the community's threshold of quality. You may see it by logging in.