I am trying to parse an XML file but I am getting an error when I try to read an empty tag. I have looked at the docs and I have added if statements but they dont work. Can someone help?
#!/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" ); } }
This is the input file:
<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>
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:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.