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

I am trying to pull some simple contact information out of a XML file:
<Project> <Contacts> <Contact type="Application"> <Name>Shawn Penn</Name> <WorkPhone>111-111-1111</WorkPhone> <Pager>999-999-9999</Pager> <Scope>Manager</Scope> </Contact> <Contact type="Application"> <Name>George Lucos</Name> <WorkPhone>111-111-1111</WorkPhone> <Pager>999-999-9999</Pager> <Scope>Manager</Scope> </Contact> </Contacts> </Project>
My perl program is as follows:
#!/usr/bin/perl -w use CGI qw(:standard :html :forms); use strict; use XML::XPath; use XML::XPath::XMLParser; use XML::DOM; #------------------------------- # Declare variables my $WebPage = CGI::new(); ### Create web page output des +tination ### Pull in application name of file to read from my $SourceXMLDir = "/doc/staging/ProjectInfo/" . $WebPage->param(App) +. ".xml"; ### Open XML file my $XMLFile = XML::XPath->new( filename => $SourceXMLDir ); my $ParseFile = new XML::DOM::Parser; my $XMLDOMFile = $ParseFile->parsefile ($SourceXMLDir ); #---------------END File Declaration----- ### header print $WebPage->header, $WebPage->start_html("Project Information for $ProjectName"); print "\n"; print $WebPage->h1("Contact Information for $ProjectName"), $WebPage->hr, "<table BORDER COLS=2 WIDTH=100% NOSAVE>\n", "<tr><th>Primary</th><th>Secondary</th></tr>\n", "<tr><td>", $XMLFile->find( "/Project/Contacts/Contact[\@type='Primary']/N +ame/text()" ), "</td><td>", $XMLFile->find( "/Project/Contacts/Contact[\@type='Secondary'] +/Name/text()" ), "&nbsp</td></tr>\n", "</table>\n"; my $AppNode = $XMLDOMFile->getElementsByTagName ("Contact"); my $numberNodes = $AppNode->getLength; print "<table BORDER COLS=4 WIDTH=100% >\n", "<tr><th>Scope</th><th>Name</th><th>Work Phone</th><th>Pager</ +th></tr>\n", "<tr>\n"; for (my $loopIndex = 0; $loopIndex < $numberNodes; $loopIndex++) { "<td>", my $ScopeText = $AppNode->item($loopIndex)->getFirstChild()->g +etNodeValue(); } "</td>\n</tr>", "</table>\n"; print "\n"; print $WebPage->end_html; print "\n";
The perl script runs but it does not pull in any data with the getElementsByTagName() call. It Doesn't even run the for loop. Any ideas?

Replies are listed 'Best First'.
Re: XML::DOM::Parser not getting data
by ghenry (Vicar) on Apr 05, 2005 at 18:54 UTC

    Two quick questions:

    1. What platform is this on? (mainly for the pathname)
    2. Do you have the right access permissions to read the file?

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!