undefined value problem

From: Guido (guy_langley_at_yahoo.co.uk)
Date: 02/24/04


Date: 24 Feb 2004 06:38:08 -0800

I have got an issue when I am trying to get a value that is undefined
using perl/xml. The code below falls over when it hits an element that
does not have a value.

Any ideas how can I check if the element is defined without causing
the script to fail ?

#############################################################################
Error message generated :-

Can't call method "getNodeValue" on an undefined value at
C:\Perl\work\abn\sample.pl line 12.
#############################################################################
Sample data

<ProcessingRuleGroups>
- <RuleGroup CLSID="11">
  <RuleGroupID>{1}</RuleGroupID>
  <Name>1st name</Name>
  <Description>1st desc</Description>
  </RuleGroup>
  - <RuleGroup CLSID="{22}">
  <RuleGroupID>{1}</RuleGroupID>
  <Name>2nd name</Name>
  <Description />
  </RuleGroup>
</ProcessingRuleGroups>

#############################################################################
Sample Code

use strict;
use XML::DOM;

my $parser = XML::DOM::Parser->new();
my $file = shift @ARGV;

my $doc = $parser->parsefile($file);

foreach my $RuleDetails ($doc->getElementsByTagName('RuleGroup')){
  my $PRName = $RuleDetails->getElementsByTagName('Name')->item(0)->getFirstChild->getNodeValue;
  print "Processing Rule Name :- $PRName\n";
  my $PRDesc = $RuleDetails->getElementsByTagName('Description')->item(0)->getFirstChild->getNodeValue;
  print "Processing Rule Groups :- $PRDesc\n";
}