in reply to How might one determine empty XML payload?

Hi vlturner,

Here's one way:

my $xml = q{<?xml version="1.0" encoding="UTF-8"?><response></response +>}; my $dom = XML::LibXML->load_xml(string=>$xml); my $isEmpty = !$dom->documentElement->nonBlankChildNodes; print "isEmpty: ", ($isEmpty?"yes":"no"), "\n"; __END__ isEmpty: yes

This method has the advantage that whitespace inside the <response> tag is ignored. If you don't want that, and want to know specifically when the <response> tag has no content at all, use hasChildNodes instead of nonBlankChildNodes.

Hope this helps,
-- Hauke D

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

    I'm a bit confused here. $input_xml should change to the value you set in $xml when my database request gets to the end of the table. So I substituted $xml for $input_xml and I am getting the following error. Not sure how to use the library correctly yet.

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

      Hi vlturner,

      XML::LibXML->load: specify location, string, or IO at ...

      What does the code look like that is causing this error? Maybe $input_xml is undef (Data::Dumper), or perhaps you wrote XML::LibXML->load_xml($input_xml) by accident?

      Regards,
      -- Hauke D

        Here is the code.

        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"; say $fh $input_xml; $offset = $offset + $record_limit +50000 ; print "Offset=$offset\n"; } while ($input_xml = Get_CMDB_CI()); close($fh) || warn "close failed: $!";