This is the input file:#!/usr/bin/perl #use warnings; #use strict; use XML::DOM; use LWP::UserAgent; use LWP::Simple; open( CONTACTS, "+<contacts2.xml" ) or die( "Error opening contact.xml" ); my $parser = new XML::DOM::Parser; my $document = $parser->parse( \*CONTACTS ); printlist( $document ); seek( CONTACTS, 0, 0 ); truncate( CONTACTS, 0 ); $document->print( \*CONTACTS ); sub printlist { my $document = shift; my $root = $document->getElementsByTagName( "ResultSet" )->item( 0 ); my $contactList = $root->getChildNodes(); for my $i ( 1 .. $contactList->getLength - 1 ) { my $contact = $contactList->item( $i ); next unless ( $contact->getNodeName eq 'Result' ); my $Title = $contact->getElementsByTagName( "Title" ); my $title = $Title->item( 0 )->getChildAtIndex( 0 )->getData(); my $Address = $contact->getElementsByTagName( "Address" ); my $address = $Address->item( 0 )->getChildAtIndex( 0 )->getData +(); my $City = $contact->getElementsByTagName( "City" ); my $city = $City->item( 0 )->getChildAtIndex( 0 )->getData(); my $State = $contact->getElementsByTagName( "State" ); my $state = $State->item( 0 )->getChildAtIndex( 0 )->getData(); my $Phone = $contact->getElementsByTagName( "Phone" ); my $phone = $Phone->item( 0 )->getChildAtIndex( 0 )->getData(); my $Lat = $contact->getElementsByTagName( "Latitude" ); my $lat = $Lat->item( 0 )->getChildAtIndex( 0 )->getData(); my $Long = $contact->getElementsByTagName( "Longitude" ); my $long = $Long->item( 0 )->getChildAtIndex( 0 )->getData(); $BusUrl = $contact->getElementsByTagName( "BusinessUrl" ); $bus_url = $BusUrl->item( 0 )->getChildAtIndex( 0 )->getData(); # change phone number format $phone =~ s/\(//; $phone =~ s/\)//; $phone =~ s/-//; $phone =~ s/\s+//; print( "$title $address $city $state $phone $lat $long $bus_url\ +n" ); } }
I get an error when it tries to getData for Johns Bakery because the <BusinessUrl></BusinessUrl> is empty. Error message: Can't call method "getData" on an undefined value at parse_xml.pl line 74. These do not work:<ResultSet> <Result> <Title>My Real Estate Agent</Title> <Address>74738 Jones Ave</Address> <City>Leesburg</City> <State>VA</State> <Phone>(703) 723-1113</Phone> <Latitude>84848894</Latitude> <Longitude>23232</Longitude> <BusinessUrl>http://www.mcmmcmmc.net</BusinessUrl> </Result> <Result> <Title>Johns Bakery</Title> <Address>8484399 heewis Dr</Address> <City>Sterling</City> <State>VA</State> <Phone>(703) 723-1114</Phone> <Latitude>8383883</Latitude> <Longitude>213123</Longitude> <BusinessUrl></BusinessUrl> </Result> <Result> <Title>Kevins flooring</Title> <Address>84848484 kevin drive</Address> <City>Fairfax</City> <State>VA</State> <Phone>(703) 723-1115</Phone> <Latitude>2321321</Latitude> <Longitude>123</Longitude> <BusinessUrl>http://www.nothing.com</BusinessUrl> </Result> </ResultSet>
if( $#BusUrl < 0 ) if( length $contact->getElementsByTagName( "BusinessUrl" )) if ($BusUrl) if (defined ( $BusUrl )) if (defined ( $contact->getElementsByTagName( "BusinessUrl" ))) if ($BusUrl) if ($BusUrl->item( 0 )->getChildAtIndex( 0 )->getLength)
In reply to use XML::DOM - what to do with empty tags? by kevyt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |