Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: XML::Twig n00b

by Anonymous Monk
on Oct 23, 2009 at 03:42 UTC ( #802830=note: print w/ replies, xml ) Need Help??


in reply to XML::Twig n00b

#!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $xml = <<'__XML__'; <?xml version="1.0" encoding="UTF-8"?> <authenticationReports> <generatedTime>Tue Sep 29 07:07:34 PDT 2009</generatedTime> <appDeploymentFile name="app-deployment.properties.hklcp.trading"> <application name="hk"> <urlInfo> <url>e/t/hk/accts_subscription</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/accts_forms</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/custtradingpage</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/accts_userinfo</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/headermain</url> </urlInfo> <urlInfo> <url>e/t/hk/custservicepage</url> </urlInfo> <urlInfo> <url>e/t/hk/accts_transfermoney</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/userprereq</url> </urlInfo> <urlInfo> <url>e/t/hk/indices_us</url> </urlInfo> <urlInfo> <url>e/t/hk/homeloggedmessage</url> </urlInfo> <urlInfo> <url>e/t/hk/lead</url> </urlInfo> <urlInfo> <url>e/t/hk/orderviewmin</url> </urlInfo> <urlInfo> <url>e/t/hk/accts_changelogin</url> <otherPrereq>SessionPreReq</otherPrereq> </urlInfo> </application> <application name="intl"> <urlInfo> <url>e/t/intl/quotesandresearch</url> </urlInfo> <urlInfo> <url>e/t/intl/intltablesubnavviewcomponent</url> </urlInfo> <urlInfo> <url>e/t/intl/intltablemetaviewcomponent</url> </urlInfo> <urlInfo> <url>e/t/intl/disclaimer</url> </urlInfo> <urlInfo> <url>e/t/intl/headermain</url> </urlInfo> <urlInfo> <url>e/t/intl/indices_us</url> </urlInfo> <urlInfo> <url>e/t/intl/lead</url> </urlInfo> <urlInfo> <url>e/t/intl/selectlanguage</url> </urlInfo> <urlInfo> <url>e/t/intl/get-screen</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/intl/page_f</url> </urlInfo> <urlInfo> <url>e/t/intl/basicprereq</url> </urlInfo> <urlInfo> <url>e/t/intl/page</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> </application> </appDeploymentFile> </authenticationReports> __XML__ { my $name = ""; my %apps; my %urls; my $t = new XML::Twig( start_tag_handlers => { 'appDeploymentFile/application' => sub { my ( $twig, $tag, %att ) = @_; #~ %att is only defined when twig_roots #~ $_ is only defined when twig_handlers #~ twig_handlers is uses less memory $name = $att{name} || $_->{'att'}->{name}; return; }, }, twig_handlers => { #~ twig_roots => { 'appDeploymentFile/application/urlInfo/url' => sub { #~ warn "root _ = $_ |||| @_ "; warn $_->text;; $apps{$name}{ "/" . $_->text }++; $urls{ "/" . $_->text } = $name; return; }, }, ); $t->parse($xml); undef $t; use Data::Dumper(); print Data::Dumper->new([ \%apps, \%urls ])->Indent(1)->Dump; } __END__ $VAR1 = { 'intl' => { '/e/t/intl/lead' => 1, '/e/t/intl/page_f' => 1, '/e/t/intl/get-screen' => 1, '/e/t/intl/basicprereq' => 1, '/e/t/intl/indices_us' => 1, '/e/t/intl/intltablesubnavviewcomponent' => 1, '/e/t/intl/selectlanguage' => 1, '/e/t/intl/page' => 1, '/e/t/intl/quotesandresearch' => 1, '/e/t/intl/intltablemetaviewcomponent' => 1, '/e/t/intl/disclaimer' => 1, '/e/t/intl/headermain' => 1 }, 'hk' => { '/e/t/hk/orderviewmin' => 1, '/e/t/hk/indices_us' => 1, '/e/t/hk/accts_subscription' => 1, '/e/t/hk/userprereq' => 1, '/e/t/hk/custtradingpage' => 1, '/e/t/hk/custservicepage' => 1, '/e/t/hk/accts_changelogin' => 1, '/e/t/hk/accts_transfermoney' => 1, '/e/t/hk/homeloggedmessage' => 1, '/e/t/hk/lead' => 1, '/e/t/hk/accts_forms' => 1, '/e/t/hk/accts_userinfo' => 1, '/e/t/hk/headermain' => 1 } }; $VAR2 = { '/e/t/intl/lead' => 'intl', '/e/t/intl/page_f' => 'intl', '/e/t/hk/userprereq' => 'hk', '/e/t/hk/custservicepage' => 'hk', '/e/t/intl/indices_us' => 'intl', '/e/t/intl/selectlanguage' => 'intl', '/e/t/intl/intltablemetaviewcomponent' => 'intl', '/e/t/intl/quotesandresearch' => 'intl', '/e/t/intl/headermain' => 'intl', '/e/t/intl/disclaimer' => 'intl', '/e/t/hk/accts_forms' => 'hk', '/e/t/hk/accts_userinfo' => 'hk', '/e/t/hk/accts_subscription' => 'hk', '/e/t/hk/indices_us' => 'hk', '/e/t/hk/orderviewmin' => 'hk', '/e/t/intl/get-screen' => 'intl', '/e/t/hk/custtradingpage' => 'hk', '/e/t/intl/basicprereq' => 'intl', '/e/t/hk/accts_changelogin' => 'hk', '/e/t/hk/accts_transfermoney' => 'hk', '/e/t/hk/homeloggedmessage' => 'hk', '/e/t/intl/intltablesubnavviewcomponent' => 'intl', '/e/t/intl/page' => 'intl', '/e/t/hk/lead' => 'hk', '/e/t/hk/headermain' => 'hk' };


Comment on Re: XML::Twig n00b
Download Code
Replies are listed 'Best First'.
Re^2: XML::Twig n00b
by Anonymous Monk on Oct 23, 2009 at 03:45 UTC
Re^2: XML::Twig n00b
by Binford (Sexton) on Oct 23, 2009 at 04:24 UTC
    I've got to study that more, but can I put the otherPrereq tag in the hash? THANKS though...
      Sure, but you'll have to decide on a data structure
      #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $xml = <<'__XML__'; <?xml version="1.0" encoding="UTF-8"?> <authenticationReports> <generatedTime>Tue Sep 29 07:07:34 PDT 2009</generatedTime> <appDeploymentFile name="app-deployment.properties.hklcp.trading"> <application name="hk"> <urlInfo> <url>e/t/hk/accts_subscription</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/accts_forms</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/custtradingpage</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/accts_userinfo</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/headermain</url> </urlInfo> <urlInfo> <url>e/t/hk/custservicepage</url> </urlInfo> <urlInfo> <url>e/t/hk/accts_transfermoney</url> <otherPrereq>HKPwdPreReq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/hk/userprereq</url> </urlInfo> <urlInfo> <url>e/t/hk/indices_us</url> </urlInfo> <urlInfo> <url>e/t/hk/homeloggedmessage</url> </urlInfo> <urlInfo> <url>e/t/hk/lead</url> </urlInfo> <urlInfo> <url>e/t/hk/orderviewmin</url> </urlInfo> <urlInfo> <url>e/t/hk/accts_changelogin</url> <otherPrereq>SessionPreReq</otherPrereq> </urlInfo> </application> <application name="intl"> <urlInfo> <url>e/t/intl/quotesandresearch</url> </urlInfo> <urlInfo> <url>e/t/intl/intltablesubnavviewcomponent</url> </urlInfo> <urlInfo> <url>e/t/intl/intltablemetaviewcomponent</url> </urlInfo> <urlInfo> <url>e/t/intl/disclaimer</url> </urlInfo> <urlInfo> <url>e/t/intl/headermain</url> </urlInfo> <urlInfo> <url>e/t/intl/indices_us</url> </urlInfo> <urlInfo> <url>e/t/intl/lead</url> </urlInfo> <urlInfo> <url>e/t/intl/selectlanguage</url> </urlInfo> <urlInfo> <url>e/t/intl/get-screen</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> <urlInfo> <url>e/t/intl/page_f</url> </urlInfo> <urlInfo> <url>e/t/intl/basicprereq</url> </urlInfo> <urlInfo> <url>e/t/intl/page</url> <otherPrereq>BasicPrereq</otherPrereq> </urlInfo> </application> </appDeploymentFile> </authenticationReports> __XML__ # REUSING $xml # SEE http://search.cpan.org/perldoc?XML::Twig#xparse # $xml = appDeploymentFile('xmlexample.xml'); $xml = appDeploymentFile($xml); use Data::Dumper(); print Data::Dumper->new([ $xml ])->Indent(1)->Dump; sub appDeploymentFile { my( $xml ) = @_; my $url = ""; my %urls; my $appFileName = ""; my $t = new XML::Twig( start_tag_handlers => { 'appDeploymentFile' => sub { my ( $twig, $tag, %att ) = @_; #~ %att is only defined when twig_handlers #~ $_ is only defined when twig_roots #~ twig_handlers is uses less memory $appFileName = $att{name} || $_->{'att'}->{name}; return; }, }, twig_handlers => { 'appDeploymentFile/application/urlInfo/url' => sub { $url = "/" . $_->text ; $urls{ $url } = ""; return; }, 'appDeploymentFile/application/urlInfo/otherPrereq' => sub { $urls{ $url } = $_->text ; return; }, }, ); $t->xparse($xml); undef $t; # REUSING $url $url = join ":", "prd" , $1 , "web" , $2 if $appFileName =~ /\.([^\. +]+?)\.([^\.]+?)$/; return [ $appFileName , $url, \%urls ]; } __END__ $VAR1 = [ 'app-deployment.properties.hklcp.trading', 'prd:hklcp:web:trading', { '/e/t/intl/lead' => '', '/e/t/intl/page_f' => '', '/e/t/hk/userprereq' => '', '/e/t/hk/custservicepage' => '', '/e/t/intl/indices_us' => '', '/e/t/intl/selectlanguage' => '', '/e/t/intl/intltablemetaviewcomponent' => '', '/e/t/intl/quotesandresearch' => '', '/e/t/intl/headermain' => '', '/e/t/intl/disclaimer' => '', '/e/t/hk/accts_forms' => 'HKPwdPreReq', '/e/t/hk/accts_userinfo' => 'HKPwdPreReq', '/e/t/hk/accts_subscription' => 'HKPwdPreReq', '/e/t/hk/indices_us' => '', '/e/t/hk/orderviewmin' => '', '/e/t/intl/get-screen' => 'BasicPrereq', '/e/t/hk/custtradingpage' => 'BasicPrereq', '/e/t/intl/basicprereq' => '', '/e/t/hk/accts_changelogin' => 'SessionPreReq', '/e/t/hk/accts_transfermoney' => 'HKPwdPreReq', '/e/t/hk/homeloggedmessage' => '', '/e/t/intl/intltablesubnavviewcomponent' => '', '/e/t/intl/page' => 'BasicPrereq', '/e/t/hk/lead' => '', '/e/t/hk/headermain' => '' } ];
        No still not working :( There are MULTIPLE AppDeployMentFile entries each with 1 to N application entries, each with 1 to N urlInfo sections. I somehow need to call this multiple times on each appDeploymentFile "chunk"? Thanks for your help though! Appreciate it. NEver mind, I just used xml_split on the appDeploymentFile tag and will prcoess each file from that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://802830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2015-07-07 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls