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' => '' } ];

In reply to Re^3: XML::Twig n00b by Anonymous Monk
in thread XML::Twig n00b by Binford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.