in reply to Re: use XML::DOM - what to do with empty tags?
in thread use XML::DOM - what to do with empty tags?

Thanks, That makes a lot of sense. I am still getting the error:
Can't call method "getData" on an undefined value at parse_xml.pl line + 87.
New code:
my $Long = $contact->getElementsByTagName( "Longitude" ); my $long = $Long->item(0)->getChildAtIndex(0)->getData(); # declare lexical variable *outside the scope of the eval* my $BusUrl; # trap potential error eval { $BusUrl = $contact->getElementsByTagName( "BusinessUrl" ); }; # check if tag retrieval failed if($@) { # assign an empty (but defined) string #$BusUrl = ''; } $bus_url = $BusUrl->item(0)->getChildAtIndex(0)->getData(); # line 87 # change phone number format

Replies are listed 'Best First'.
Re^3: use XML::DOM - what to do with empty tags?
by EvanK (Chaplain) on Jan 03, 2007 at 16:57 UTC
    ah, the example code I provided just assigns an empty string to $BusUrl, which you later use as an object (my bad). so really, what you'd want to do is something like this:
    if($@) { # assign an empty (but defined) string to $bus_url $bus_url = ''; } else { # assign tag contents to $bus_url $bus_url = $BusUrl->item(0)->getChildAtIndex(0)->getData(); }
    what you really want to do is follow mirod's advice and use the hasChildNodes method (note that he uses a much more flexible implementation than the following):
    my $BusUrl = $contact->getElementsByTagName( "BusinessUrl" ); # check if tag is defined and has children if (defined $BusUrl && $BusUrl->hasChildNodes) { $bus_url = $BusUrl->item( 0 )->getChildAtIndex( 0 )->getData(); } # otherwise assign an empty string else { $bus_url = ''; }

    __________
    The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
    - Terry Pratchett