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

Hello Monks I am trying to extract information from xml file. The XML file looks like:
<?xml version="1.0"?> <reports> <report> <report-name>POSM1M029</report-name> <source-file id='1'> <source-file-path></source-file-path> </source-file> <destination-file id='1'> <destination-file-path></destination-file-path> </destination-file> </report> <report> <report-name>CNAM1D001</report-name> <source-file id='1'> <source-file-path>G:\Interfaces\ApplnData\DENVER_TRAN\Navi +Sys\Input\EXPORT_CFM.txt</source-file-path> </source-file> <destination-file id='1'> <destination-file-path>L:\Enterprise Reports\Input Files\ +ipCNAM1D001Confirm.txt</destination-file-path> </destination-file> <source-file id='2'> <source-file-path>G:\Interfaces\ApplnData\DENVER_TRAN\Navi +sys\Input\EXPORT_ERR.txt</source-file-path> </source-file> <destination-file id='2'> <destination-file-path>L:\Enterprise Reports\Input Files\ +ipCNAM1D001Error.txt</destination-file-path> </destination-file> </report> </reports>
And my code goes as
use Win32::OLE qw(in with); #my $xml_file = 'file.xml'; my $DOM_document = Win32::OLE->new('MSXML2.DOMDocument') or die "new() + failed"; $DOM_document->{async} = "False"; $DOM_document->{validateOnParse} = "True"; my $boolean_Load = $DOM_document->Load("file.xml"); if (!$boolean_Load) { die "topten.xml did not load"; } my $Reports = $DOM_document->DocumentElement(); my $Report = $Reports->childNodes(); foreach my $Event (in $Report) # make sure you include the 'in' { my $temp =$Event->Attributes->getNamedItem("report-name")->{text}; print $temp; }
I want to get the report names (POSM1M029,CNAM1D001 etc. Please tell me where I am wrong. Thanks for your help.

Replies are listed 'Best First'.
Re: Extracting information from XML file
by Cody Pendant (Prior) on Apr 05, 2005 at 02:28 UTC
    Don't know how to do it "your" way, but this seems to work:
    #!/usr/bin/perl use strict; use warnings; use diagnostics; use XML::Simple; my $data = XMLin("/path/to/report.xml"); for ( @{ $data->{'report'} } ) { print "Report name: $_->{'report-name'}\n"; }


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print
      Hi Thanks a lot for your help. The method you suggested worked fine, but I am still confused as to how to go one level deeper into it. Like how to extract the values of source path and destination path. I only get hash values and not the real values. Please help.
        OK I'm a bit busy.

        This works but it's ugly and doesn't run under strict for some reason I'll figure out later.

        #!/usr/bin/perl use XML::Simple; my $data = XMLin("/path/to/report.xml"); for ( @{ $data->{'report'} } ) { print "Report name: $_->{'report-name'}\n"; foreach my $key ( keys( %{ $_->{'source-file'} } ) ) { print "source file path: " . $_->{'source-file'}->{"$key"} ->{'source-file-path'} . "\n"; } foreach my $key ( keys( %{ $_->{'destination-file'} } ) ) { print "destination file path: " . $_->{'destination-file'}->{"$key"} ->{'destination-file-path'} . "\n"; } }

        The thing I'd do, if it were me, is mess about a bit with the options of XML::Simple, so that the data structure was a bit easier to access.



        ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
        =~y~b-v~a-z~s; print
Re: Extracting information from XML file
by ikegami (Patriarch) on Apr 05, 2005 at 00:50 UTC

    I've never used Win32::OLE before or this object, but I'm guessing it's:

    my $reports = $DOM_document->DocumentElement(); foreach my $report (in $reports->childNodes()) { foreach my $report_child (in $report->childNodes()) { if ($report_child->{'nodeName'} eq 'report-name') { print($report_child->text(), "\n"); next; } } }

    Update: I initially forgot to put in the calls to childNodes. Fixed. Tested. Works.

    By the way, the following is a better error message for loading:
    my $DOM_document = Win32::OLE->new('MSXML2.DOMDocument')
    or die("Unable to load object MSXML2.DOMDocument: $^E\n");
    It's more descriptive, and the $^E will display why it wasn't able to load the object.

Re: Extracting information from XML file
by mirod (Canon) on Apr 05, 2005 at 08:13 UTC

    If you install XML::Twig you can also do:

    xml_grep -t report-name file.xml