use Data::Dumper 'DumperX';
use XML::Twig;
my $ARGH = q{
podmaster
};
print DumperX teh_xml_to_hashref($ARGH);
print DumperX teh_xml_to_hashref('oh how I love thee');
print DumperX teh_xml_to_hashref('babies>');
die teh_hashref_to_xml(teh_xml_to_hashref($ARGH));
sub teh_xml_to_hashref {
my $xml = shift;
my $t = new XML::Twig;
$t->safe_parse($xml);
return { crack => babies => 6 => 6 } if $@;
$t = $t->root; # there is only NODE
return {
content => $t->text, # cause it's not an att
map { $_ => $t->att($_) } $t->att_names,
};
}
# where is the love? the above in under 10 minutes, but this?
sub teh_hashref_to_xml {
my $xref = shift;
my $t = new XML::Twig;
$t->parse('');
$t = $t->root; # there is only NODE
$t->set_text($xref->{content});
delete $xref->{content};
for my $att(keys %$xref) {
# $t->set_att( $key => $$xref{$key} );
# d'oh, where'em crack babies at?:)
$t->set_att( $att => $$xref{$att} );
}
return $t->print;
}
__END__
$VAR1 = {
'reputation' => '0',
'createtime' => '2001-08-24 08:01:08',
'content' => 'podmaster',
'id' => '107642'
};
$VAR1 = {
'content' => 'oh how I love thee',
'dear' => 'xmltwig'
};
$VAR1 = {
'crack' => 'babies',
'6' => 6
};
podmasterDied at twig.t line 11.