in reply to Re^4: How might one determine empty XML payload?
in thread How might one determine empty XML payload?

Make use of your flag to exit the loop.

my $input_xml; open my $fh, '>>:encoding(utf8)', $outputfile or die "Cannot open: $ou +tputfile: $!"; do { my $dom = XML::LibXML->load_xml(string=>$input_xml); my $isEmpty = !$dom->documentElement->hasChildNodes; print "isEmpty: ", ($isEmpty?"yes":"no"), "\n"; last if $isEmpty; say $fh $input_xml; $offset = $offset + $record_limit +50000 ; print "Offset=$offset\n"; } while ($input_xml = Get_CMDB_CI()); close($fh) || warn "close failed: $!";
But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Replies are listed 'Best First'.
Re^6: How might one determine empty XML payload?
by vlturner (Sexton) on Jun 02, 2016 at 17:15 UTC

    I modified the code, but still get the error. I am doing something wrong with XML::LibXML.

    XML::LibXML->load: specify location, string, or IO at ./ServiceNowCMDB_ALL_CI_DumpXML.pl line 99.

    #!/usr/bin/perl use strict; use warnings; use 5.014; use MIME::Base64; use Data::Dumper; use utf8; use Text::Unidecode; use XML::Twig; use XML::LibXML; use REST::Client; use Data::Validate::Domain qw(is_hostname); use Socket; use Net::DNS; use Net::MAC; # Just in case we want to request JSON instead of XML from CMDB #use JSON; Just in case we want to request JSON instead of XML from CM +DB my $scriptversion = "1.01 (Vance Turner, Staples Inc., 1/20/2016)"; our $header_count = 0; our $fqdn_pos = 0; our $ip_address_pos = 0; our $dns_domain_pos = 0; our $name_pos = 0; our $mac_address_pos = 0; # declare our command line options variables. our $host = 'https://staplessb.service-now.com'; our $wanthelp = 0; our $record_limit = 1; our $class = ''; our $user = ''; our $pwd = ''; our $offset = 0; our $outputfile = 'XMLDUMP.xml'; sub usage { my $err = shift and select STDERR; print <<EOU; usage: perl $0 [ -t URL ] [-r <max_records>] [-c <class>] [-u username + ] [-p password] [-o <outputfilename>] -t <host_url> use <host_url> for the CMDB connection to op +en. (https://staplessb.service-now.com) -r <record_limit> use <record_limit> as the maximum nomber of +CMDB records to retrieve, default = '100' -c <class> use <class> as the CMDB class records you wa +nt to retrieve. -u <username> CMDB username to use -p <password> CMDB password to use -o <outputfilename> write output to file named <outputfilename>, + defaults to stdout if no outputfile is specified Examples: perl $0 -u user -p password -r max_records -o outputfile.xml (cre +ates outputfile.xml) EOU exit $err; } # usage sub Get_CMDB_CI { my $client = REST::Client->new(host => $host); my $encoded_auth = encode_base64("$user:$pwd", ''); $client->GET("/api/now/table/cmdb_ci?sysparm_limit=$record_limit&syspa +rm_offset=$offset", {'Authorization' => "Basic $encoded_auth", 'Accept' => 'application/xml'}); my $CI_RECS = $client->responseContent(); return ($CI_RECS); } use Getopt::Long qw(:config bundling nopermute passthrough); GetOptions ( "help|h|?" => \$wanthelp , "t=s" => \$host, "r=i" => \$record_limit, "c=s" => \$class, "u=s" => \$user, "p=s" => \$pwd, "o=s" => \$outputfile, ); if ($wanthelp) { usage(1); } if (-s $outputfile) { print STDERR "File '$outputfile' already exists. Overwrite? [y/N] +> N\b"; scalar <STDIN> =~ m/^[yj](es|a)?$/i or exit; } my $input_xml; open my $fh, '>>:encoding(utf8)', $outputfile or die "Cannot open: $ou +tputfile: $!"; do { my $dom = XML::LibXML->load_xml(string=>$input_xml); my $isEmpty = !$dom->documentElement->hasChildNodes; last if $isEmpty; say $fh $input_xml; $offset = $offset + $record_limit +50000 ; print "Offset=$offset\n"; } while ($input_xml = Get_CMDB_CI()); close($fh) || warn "close failed: $!";

      Found the problem. The while loop may not have a value populated, and the do has to await new data. Testing that when the payload is empty it terminates with the exit, since last causes an error.

      do { if ( $input_xml ) { $dom = XML::LibXML->load_xml( string => $input_xml ); my $isEmpty = !$dom->documentElement->hasChildNodes(); last if $isEmpty; say $fh $input_xml; $offset = $offset + $record_limit; say $offset; } } while ($input_xml = Get_CMDB_CI()); close($fh) || warn "close failed: $!";