Re: Help with accessing an unknown set of data generated by XML::Simple

Francisco Obispo <[email protected]> Fri, 17 Feb 2012 22:13:06 -0800
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
On Feb 17, 2012, at 6:35 PM, Dale Puckett wrote:
> ...
> 
> If I could get these data pairs I have the support code to finish the rest
> of he project.


And it seems like that's exactly what XML::Simple is providing for you.. if you look at it's structure:

$ ./parser.pl Westward_LNG-JobNetworkFinal.xml 
$VAR1 = {
          'JOB' => {
                   'UPSELL' => {
                               'PRIORITY' => 'db:adOCFlag41',
                               'FEATURED' => 'db:adOCFlag39',
                               'TOTAL_TALENT_REACH' => 'db:adOCFlag40'
                             },
                   'WEB_ACCOUNT' => {
                                    'COMPANY' => 'db:company',
                                    'EMAIL' => 'db:email'
                                  },
                   'CITY' => 'webtag:ALL_CITY',
                   'ACCTID' => 'db:accountID',
                   'PUBID' => 'db:pubID',
                   'CATEGORY' => 'db:classID',
                   'CONFIDENTIAL_COMPANY' => 'webtag:CONFIDENTIAL',
                   'TEMPWEBFORMTEXT' => 'webtag:ALL_AD_TEXT',
                   'AD_TYPE' => 'db:adCode',
                   'APPLY_DATA' => {
                                   'APPLY_URL' => 'webtag:COMPANY_WEB',
                                   'FIRST_NAME' => 'webtag:APPLY_FIRST',
                                   'APPLY_EMAIL' => 'webtag:JOB_EMAIL',
                                   'LAST_NAME' => 'webtag:APPLY_LAST',
                                   'PHONE' => 'webtag:CONTACT'
                                 },
                   'PDF' => 'addquery:displayFilelink',
                   'JOB_ID' => 'db:adID',
                   'TITLE' => 'webtag:JOB_TITLE',
                   'ZIP' => 'webtag:ALL_ZIP',
                   'STATE' => 'webtag:ALL_STATE',
                   'DESCRIPTION' => 'addquery:whichText',
                   'TEMPRAWTEXT' => 'addquery:rawText',
                   'COMPANY_NAME' => 'webtag:COMPANY_NAME'
                 }
        };


What you have is a HASHREF that has all of the elements present in the XML document. You also have a structure which is similar to the XML structure in the file.

Now, if what you are looking is for particular keys, I think you can do something like this:


#!/usr/bin/env perl
use common::sense;
use XML::Simple;
use Data::Dumper qw(Dumper);

my @WANTED=qw(CITY ZIP STATE DESCRIPTION COMPANY_NAME);
my $xml = XMLin( $ARGV[0] );

my %values=map{$_ => &lookup_key($_,$xml)} @WANTED;

print Dumper(\%values);

sub lookup_key {
  my ( $key, $hashref ) = @_;
  foreach my $k ( keys %{$hashref} ) {
    if ( ref $hashref->{$k} ~~ q{HASH} ) {
      my $val=&lookup_key( $key, $hashref->{$k} );
      return $val if $val;
    }
    else {
      return $hashref->{$k} if $k ~~ $key;
    }
  }
}

For which the result will be:

$ ./parser.pl Westward_LNG-JobNetworkFinal.xml 
$VAR1 = {
          'ZIP' => 'webtag:ALL_ZIP',
          'CITY' => 'webtag:ALL_CITY',
          'DESCRIPTION' => 'addquery:whichText',
          'STATE' => 'webtag:ALL_STATE',
          'COMPANY_NAME' => 'webtag:COMPANY_NAME'
        };



Hopefully that will help a little bit.


Francisco Obispo 
email: [email protected]
Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
PGP KeyID = B38DB1BE

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs