in reply to use XML::DOM - what to do with empty tags?
When you write $bus_url = $BusUrl->item( 0 )->getChildAtIndex( 0 )->getData(); you apply a method (getData) to an undef value ($BusUrl->item( 0 )->getChildAtIndex( 0 )), hence the error. You should first check whether $BusUrl has children, throught the appropriately named hasChildNodes method.
The easiest way would be to add a sub that would return the text of the element or undef (or the empty string if it's more convenient for you). While you're at it you could also improve that sub to make it resistent to extra whitespace in the elements: if you only look at the first child of the BusinessUrl element, chances are that one day it will be a line return, with the real data on the following line (and child). Using XPath (either by switching to XML::LibXML or by using XML::DOM::XPath (which is a shameless plug)) will give you a better chance at making your code less dependent on the formating of the XML data.
You use the following (untested) code by calling my $bus_url= field( $contact, 'BusinessUrl'). Note that it is still not great, as extra comments in the data for example would be returned as part of the content of the element.
sub field { my( $parent, $tag)= @_; my @elts= $parent->getElementsByTagName( $tag); return unless( @elts); my $elt= $elts[0]; return unless( $elt->hasChildNodes); return join( '', map { $_->getData } $elt->getChildNodes); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: use XML::DOM - what to do with empty tags?
by kevyt (Scribe) on Jan 03, 2007 at 16:58 UTC |