Let me start with a personal peeve. XML is a strict language spec, and it has formal definitions as to what is - and isn't - allowed. Therefore it's actually very easy to parse with a parser, and gets horribly messy if you try and use a hand rolled solution like a regular expression.
Not least because XML can have linefeeds and be reformatted and still be valid.
I would also suggest - don't use XML::Simple. In it's module page:
The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces.
Also - it's really important that you start a script with use strict; and use warnings;. These are really good ways to help diagnose problems and will also get much better responses if you're posting code on Stack Overflow.
With that in mind, I'd suggest picking up XML::Twig which has the ability to set twig_handlers - subroutines that are triggered to process a specific chunk of XML. In the example below - I specify twig_roots which indicates to the parser that I don't really care about anything else.
process_user is called with each User element. We test the User element for it having the appropriate attribute - and if it does, we extract the string attributes from the two subelements you're interested in.
Something like this:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
sub process_user {
my ( $twig, $user ) = @_;
if ( $user->att('text') eq "HHd5" ) {
print $user->first_child('valat')->att('string'), ":",
$user->first_child('valon')->att('string');
}
}
my $parser = XML::Twig->new( twig_roots => { 'User' => \&process_user, } );
$parser->parse( \*DATA );
__DATA__
<User text="HHd5">
<max string="0"/>
<min string="pick up"/>
<valat string="0"/>
<valon string="0"/>
<time string="GMT"/>
</User>
But simplifying a bit perhaps, to make it similar to your existing code:
use strict;
use warnings;
use XML::Twig;
my $xml_twig = XML::Twig->new();
$xml_twig->parsefile("test.xml");
foreach my $user ( $xml_twig->root->children('User') ) {
if ( $user->att('text') eq "HHd5" ) {
print $user ->first_child('valat')->att('string');
print ":";
print $user ->first_child('valon')->att('string');
}
}
(NB: The example above doesn't quite work with your XML snippet, but that's because I'm assuming that User isn't your root node in your XML. It couldn't be really. )
XML::Simple: search.cpan.org/~grantm/XML-Simple-2.20/lib/XML/… Specifically: "The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. "