perlDevsWorld has asked for the wisdom of the Perl Monks concerning the following question:

I need help in extracting some information from xml file using perl. 1) test.xml file How can I extract hostname1.xxx.com(hostname) and key and tier and then put it in one file ( a text file ) using perl scripts
<httpsRoutes> <httpsRoute hostname="hostname1.xxx.com" port="443" environment="Q +A" tier="dmz"> <key>6924</key> </httpsRoute> <httpsRoute hostname="hostname3.xxx.com" port="7416" environment=" +CAT" tier="dmz"> <key>9990068</key> </httpsRoute> </httpsRoutes>
  • Comment on extracting attribute values which might have multiple occurences from xml file
  • Download Code

Replies are listed 'Best First'.
Re: extracting attribute values which might have multiple occurences from xml file
by toolic (Bishop) on Jan 30, 2015 at 19:26 UTC
    XML::Twig
    use warnings; use strict; use XML::Twig; my $twig = XML::Twig->new( twig_handlers => { httpsRoute => \&httpsRou +te } ); $twig->parsefile('test.xml'); sub httpsRoute { my ( $twig, $httpsRoute ) = @_; print $httpsRoute->att('hostname'), "\n"; print $httpsRoute->att('tier'), "\n"; print $httpsRoute->first_child('key')->text(), "\n"; print "\n"; } __END__ hostname1.xxx.com dmz 6924 hostname3.xxx.com dmz 9990068

    Use open to print to a file.

      Thanks a bunch!. This is working like a magic. Great help. Thanks a ton!.
Re: extracting attribute values which might have multiple occurences from xml file
by choroba (Cardinal) on Jan 30, 2015 at 20:19 UTC
    Using XML::XSH2:
    open test.xml ; for /httpsRoutes/httpsRoute echo @hostname (key) @tier | cat > out.txt
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      @choroba: Thanks so much for your response!. Looks interesting wrt xpath look a like. But running into below errors ( modified a little)
      #! perl -w use warnings; use strict; use XML::XSH2; my $tempVal = XML::XSH2->new( twig_handlers => { /httpsRoutes/httpsRou +te } ); $tempVal->parsefile("c:\\location\\test.xml"); for ($tempVal) { echo @hostname (key) @tier | cat > out.txt; }
      errors:

      C:\Strawberry\perl\bin>perl "C:\location\xmlRead_2.pl" Array found where operator expected at C:\location\xmlRead_2.pl line 10, at end of line (Missing operator before ?) Array found where operator expected at C:\location\xmlRead_2.pl line 10, at end of line (Missing operator before ?) Unknown regexp modifier "/h" at C:\location\xmlRead_2.pl line 6, at end of line Unknown regexp modifier "/t" at C:\location\xmlRead_2.pl line 6, at end of line Unknown regexp modifier "/t" at C:\location\xmlRead_2.pl line 6, at end of line Unknown regexp modifier "/R" at C:\location\xmlRead_2.pl line 6, at end of line Unknown regexp modifier "/t" at C:\location\xmlRead_2.pl line 6, at end of line Unknown regexp modifier "/e" at C:\location\xmlRead_2.pl line 6, at end of line syntax error at C:\location\xmlRead_2.pl line 10, near "echo @hostname " Global symbol "@hostname" requires explicit package name at C:\location\xmlRead_2.pl line 10. Global symbol "@tier" requires explicit package name at C:\location\xmlRead_2.pl line 10. Execution of C:\location\xmlRead_2.pl aborted due to compilation errors.

        You are mixing XML::Twig with XML::XSH2.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ