Iam trying to read a large xml file which have size 200 mb in perl but when Iam trying to execute this file using linux command it freeze and not inserted into database. Iam trying to read the xml file like following
my $i =0; use XML::LibXML; my $parser=XML::LibXML->new(); # CREATE NEW OBGECT OF LibXML my $filename = SaveXmlPath.'/NI9373hotelamenities.xml'; my $tree=$parser->parse_file($filename); # Start parsing the xml message my $root=$tree->getDocumentElement; my %hotelsList=(); my @hotelsListAmenities =(); # Retrieve the Country Code print "Now Fetch data from NI9373hotelamenities.xml"; # Loop through the cities foreach my $hotel ($root->findnodes('Hotels/Hotel')) { $hotelsListAmenities[$i]{HotelCode} = $hotel->findvalue('HotelCode'); $hotelsListAmenities[$i]{OldHotelId} = $hotel->findvalue('OldHotelId'); #$hotelsListAmenities[$i]{PAmenities} = $hotel->findvalue('PAmenities'); $hotelsListAmenities[$i]{RoomsNumber} = $hotel->findvalue('RoomsNumber'); #list of property (hotel) amenities which are separated by symbol ';' my @PAmenities = split /;/, $hotel->findvalue('PAmenities'); for(my $j=0;$j<@PAmenities;$j++) { my $id = $j+1; $hotelsListAmenities[$i]{h_facilities}[$j]{id} = $id; $hotelsListAmenities[$i]{h_facilities}[$j]{type} = 1; $hotelsListAmenities[$i]{h_facilities}[$j]{desc} = $PAmenities[$j]; } #list of room amenities which are separated by symbol ';' my @RAmenities = split /;/, $hotel->findvalue('RAmenities'); for(my $j=0;$j<@RAmenities;$j++) { my $id = $j+1; $hotelsListAmenities[$i]{r_facilities}[$j]{id} = $id; $hotelsListAmenities[$i]{r_facilities}[$j]{type} = 2; $hotelsListAmenities[$i]{r_facilities}[$j]{desc} = $RAmenities[$j]; } $i++; $hotelsList{HotelAmenities} = \@hotelsListAmenities; return @hotelsListAmenities; and this is the xml file sample
<?xml version="1.0" encoding="UTF-8"?> <XMLResponse> <ResponseType>HotelListResponse</ResponseType> <RequestInfo> <AffiliateCode>NI9373</AffiliateCode> <AffRequestId>2</AffRequestId> <AffRequestTime>2015-10-29T15:52:05</AffRequestTime> </RequestInfo> <TotalNumber>264234</TotalNumber> <Hotels> <Hotel> <HotelCode>AD0BFU</HotelCode> <OldHotelId>0</OldHotelId> <PAmenities>Bar</PAmenities> <RAmenities/> <RoomsNumber/> </Hotel> <Hotel> <HotelCode>AD0I57</HotelCode> <OldHotelId>0</OldHotelId> <PAmenities>Small pets allowed under 5 kg;YES Small pets allowed under 5 kg;Large pets allowed over 5 kg;YES Large pets allowed over 5 kg;Wheelchair-accessible;YES Wheelchair-accessible;Car park;YES Car park;Garage;YES Garage;Mobile phone coverage;Wired Internet;Wi-fi;Transfer service;Secure parking;Room service;Laundry service;Hotel safe;Cloakroom;Lift access;Newspaper stand;Supermarket;Bicycle storage;Sun terrace;Gym;Newspapers;Restaurant;Non-smoking area;Photocopier;Sun loungers;Children&amp;apos;s play area;TV lounge;Sauna;Massage;Spa treatments;Year of construction - 2008;Number of floors main building - 10;Apartments - 25;Studios - 1;Connecting rooms;YES Connecting rooms;Apartment complex;Nearest Bus / Metro Stop - 25000 m;Ski slopes - 2000 m</PAmenities> <RAmenities/> <RoomsNumber/> </Hotel> </Hotels> </XMLResponse> And Iam using this function to insert generated loop from xml file to insert into facilities table
print "Start cache Hotel Facilities"; my @facilities = cachHotelAmenities(); my ($sql,$sth)=(); ### loop on each hotel print "Count Hotel Facilities is : ".@facilities."\n"; foreach my $facility (@facilities) { my $facilitiesToInsert = ''; my $hotelID = $facility->{HotelCode}; my $roomsNumber = $facility->{RoomsNumber}; foreach my $h_facility (@{$facility->{h_facilities}}) { my $id=$h_facility->{id} ; my $type = $h_facility->{type} ; my $facility_desc = $h_facility->{desc}; $facility_desc =~ s/'/\\'/g; $facilitiesToInsert .= qq{('$id','$hotelID','$type','$facility_desc'),}; } foreach my $r_facility (@{$facility->{r_facilities}}) { my $id=$r_facility->{id}; my $type = $r_facility->{type}; my $facility_desc = $r_facility->{desc}; $facility_desc =~ s/'/\\'/g; $facilitiesToInsert .= qq{('$id','$hotelID','$type','$facility_desc'),}; } if($facilitiesToInsert){ $facilitiesToInsert =~ s/,$//; $facilitiesToInsert =~ s/\)\(/\),\(/g; my $facilitiesTable = 'HPro_hotel_facilities'; $sql = qq{insert IGNORE into $facilitiesTable(code,hotelCode,type,value) values $facilitiesToInsert }; $sth = $dbh->prepare($sql); $sth->execute() || die ("\nCan't prepare insertSuppHotelFacilities query: ".$sth->errstr."\n"); } $sql = qq{update HPro_hotels set roomcount='$roomsNumber' where code='$hotelID' }; $sth = $dbh->prepare($sql); $sth->execute() || die ("\nCan't prepare updateRoomCount query: ".$sth->errstr."\n"); sleep(1); } print "Finish cache Hotel Facilities"; Can you help me to read this large file ? what I must to do to prevent freezing ?
No comments:
Post a Comment