cajwine cajwine - 5 months ago 18x
Perl Question

Force perl XMLin to treat an empty tag as empty string?

Having the following:

perl -MXML::LibXML::Simple -MData::Dumper -E '$h=XMLin("<some><bubu>string</bubu></some>");say Dumper $h'

is parsed as:

$VAR1 = {
'bubu' => 'string'

but the

perl -MXML::LibXML::Simple -MData::Dumper -E '$h=XMLin("<some><bubu/></some>"); say Dumper $h'


perl -MXML::LibXML::Simple -MData::Dumper -E '$h=XMLin("<some><bubu></bubu></some>");say Dumper $h'


$VAR1 = {
'bubu' => {}

Is possible to get

$VAR1 = {
'bubu' => ""

To be consistent with other string values?

The real code behind the question is like:

package Something {
use Moose;
has 'bar' => (is => 'ro', isa => 'Str');
has 'baz' => (is => 'ro', isa => 'Str');

use 5.014;
use warnings;
use XML::LibXML::Simple;

my $xml = do {local $/, <DATA>};
my $hr = XMLin($xml);
for my $node( @{$hr->{node}} ) {
my $obj = Something->new($node);

which dies with

Attribute (baz) does not pass the type constraint because: Validation failed for 'Str' with value HASH(0x7f91a4a92450) at /opt/anyenv/envs/plenv/versions/5.24.0/lib/perl5/site_perl/5.24.0/darwin-2level/Moose/ line 24
Moose::Object::new('Something', 'HASH(0x7f91a3f3d430)') called at l line 28

therefore i need either

  • treat the empty
    not as
    but as

  • or adding some coercion to the package
    to coerce any empty hashrefs
    to empty strings

Any idea for the easy way?


XML::LibXML::Simple does not appear to have an option to enable this behavior.

XML::Simple does, though; set SuppressEmpty to an empty string to parse empty nodes as strings instead of containers:

# perl -MXML::Simple -MData::Dumper \
  -E '$h=XMLin("<some><bubu></bubu></some>", SuppressEmpty => ""); say Dumper $h'

$VAR1 = {
      'bubu' => ''