Created
February 24, 2014 14:03
-
-
Save pdl/9188955 to your computer and use it in GitHub Desktop.
Quickly create a new element using XML::LibXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub element { | |
my $name = shift; | |
my $element = XML::LibXML::Element->new($name); | |
my $next = shift; | |
if (ref $next eq ref {}){ | |
foreach my $attrName (keys %$next){ | |
$element->setAttribute ($attrName, $next->{$attrName}); | |
} | |
$next = shift; | |
} | |
if (defined $next) { | |
my $addChild = sub { | |
my $child = shift; | |
unless (ref $child){ | |
$child = XML::LibXML::Text->new($child) | |
} | |
$element->addChild($child) | |
}; | |
if (ref $next eq ref []){ | |
$addChild->($_) for @$next; | |
} | |
else{ | |
$addChild->($next) | |
} | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment