I'm trying to parse an XML file, if a Tag exists it should print the application "Name" attribute.
From the XML snippet if "<Timeout>900</Timeout>" exists it should print Name="App1". Of course the Timeout value could be anything.
So my thinking is to match the Tag for "Timeout" if found print 'App Name' and 'Timeout' Value.
I'm trying to use the code below, but not sure how I can recurse through all the child/parent to match the Timeout Tag (The depth isn't always the same) and then print App Name.
use strict;
use warnings;
use XML::Twig;
my $twig=XML::Twig->new(
twig_handlers => { 'Hierarchy' => \&print_elt_text
}
);
$twig->parsefile( "a.xml");
sub print_elt_text
{ my( $t, $section)= @_;
?
$t->purge;
}
XML Snippet.
<Configuration>
<Version>1.0</Version>
<User>root</User>
<Parameter ParameterName="Text">Enable</Parameter>
<Parameter ParameterName="Text2">Disable</Parameter>
<Hierarchy>
<Application Name="App1">
<Properties>
<Admin>root</Admin>
<User>root</User>
<Timeout>900</Timeout>
</Properties>
</Application>
<Application Name="App2">
<Properties>
<User>root</User>
</Properties>
</Application>
</Hierarchy>
</Configuration>
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.