in reply to XML::Twig help please

When you say <appDeploymentFile> as value I assume you want the value of the name attribute for the englobing appDeploymentFile element:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my %url2file; XML::Twig->new( twig_handlers => { url => sub { $url2file{$_->text}= $ +_->parent( 'appDeploymentFile')->att( 'name'); $_->purge; } } ) ->parsefile( "my_data.xml"); use YAML::Syck; print Dump( \%url2file);

Is this what you're looking for?

updated: give the result hash a meaningful name

Replies are listed 'Best First'.
Re^2: XML::Twig help please
by Binford (Sexton) on Oct 22, 2009 at 16:09 UTC
    Thanks for the help guys, I was almost there, when requirements changed slightly. The above code you gusy gave me worked except in the following instance:
    <authenticationReports> <generatedTime>Tue Sep 29 07:07:34 PDT 2009</generatedTime> <appDeploymentFile name="app-deployment.properties.hklcp.trading"> <application name="hk"> <urlInfo> <url>a/b/hk/accts_subscription</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>a/b/hk/accts_forms</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>a/b/hk/custtradingpage</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> <urlInfo> <url>a/b/hk/accts_userinfo</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>a/b/hk/headermain</url> </urlInfo> <urlInfo> <url>a/b/hk/custservicepage</url> </urlInfo> <urlInfo> <url>a/b/hk/accts_transfermoney</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>a/b/hk/userprereq</url> </urlInfo> <urlInfo> <url>a/b/hk/indices_us</url> </urlInfo> <urlInfo> <url>a/b/hk/homeloggedmessage</url> </urlInfo> <urlInfo> <url>a/b/hk/lead</url> </urlInfo> <urlInfo> <url>a/b/hk/orderviewmin</url> </urlInfo> <urlInfo> <url>a/b/hk/accts_changelogin</url> <otherPrereq>SessionPreReq</otherPrereq> </urlInfo> </application> <application name="intl"> <urlInfo> <url>a/b/intl/quotesandresearch</url> </urlInfo> <urlInfo> <url>a/b/intl/intltablesubnavviewcomponent</url> </urlInfo> <urlInfo> <url>a/b/intl/intltablemetaviewcomponent</url> </urlInfo> <urlInfo> <url>a/b/intl/disclaimer</url> </urlInfo> <urlInfo> <url>a/b/intl/headermain</url> </urlInfo> <urlInfo> <url>a/b/intl/indices_us</url> </urlInfo> <urlInfo> <url>a/b/intl/lead</url> </urlInfo> <urlInfo> <url>a/b/intl/selectlanguage</url> </urlInfo> <urlInfo> <url>a/b/intl/get-screen</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> <urlInfo> <url>a/b/intl/page_f</url> </urlInfo> <urlInfo> <url>a/b/intl/basicprereq</url> </urlInfo> <urlInfo> <url>a/b/intl/page</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> </application> </appDeploymentFile> </authenticationReports>
    The following code reads the <appDeploymentFile> URL's but only for the first section of <application> IOW, I get all the URL's for the section <application name ="hk"> but none for the <application name ="intl">. Here's my code now:
    sub AFXMLtoEM { print "Slurping $AFXML...."; my $TWIG = new XML::Twig ( twig_handlers => {'appDeploymentFile' = +> \&parseURL} ); #my $TWIG = new XML::Twig ( twig_handlers => {'appDeploymentFile/a +pplication' => \&parseURL} ); $TWIG -> parsefile ($AFXML) or die "Can't open $AFXML\n" ; $TWIG->flush; # Now we want to change every value from the XML name to an EM ins +tance identifier #print Dumper(\%AFURLS); exit 1; while ((my $K, my $ITEM) = each %AFURLS) { my ($G1,$G2,$APP,$INST) = split /\./,$ITEM,4; unless ($APP eq "") { $ITEM = "prd:" . $APP . ":web:" . $INST; } #Cheesy kludge - fiox when Durai confirms $AFURLS{$K} = $ITEM; } print scalar keys %AFURLS, " records slurped in.\n"; } sub parseURL { my ($T, $ADEP) = @_; my $NAME = $ADEP->att('name'); for my $URLI ($ADEP->first_child('application')->children('urlInfo +')) { #for my $URLI ($ADEP->children('urlInfo')) { # leading slash added for matching SM filters $AFURLS{ "/" . $URLI->first_child('url')->text() } = $NAME; } #$ADEP->flush; }
    How can I 1. Get all URL's in any given <appDeploymentFile> section and 2. append the <application> NAME value to the end of the $NAME (value) of the Hash? I can then parse it later. I played with various child/next_child, etc parameters, and just ain't grokking it yet. Thanks for the input. First time, I've tried to use XML::Twig before... XML::Simple had always met my needs.