use Data::Dumper 'DumperX'; use XML::Twig; my $ARGH = q{ <NODE reputation="0" createtime="2001-08-24 08:01:08" id="107642">podm +aster</NODE> }; print DumperX teh_xml_to_hashref($ARGH); print DumperX teh_xml_to_hashref('<o dear="xmltwig">oh how I love thee +</o>'); print DumperX teh_xml_to_hashref('<crack >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('<NODE></NODE>'); $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 }; <NODE createtime="2001-08-24 08:01:08" id="107642" reputation="0">podm +aster</NODE>Died at twig.t line 11.
Look ma', I'm on CPAN.
** The Third rule of perl club is a statement of fact: pod is sexy.
In reply to (podmaster: rrrr) Re: XML Simple and Tokeparser troubles
by PodMaster
in thread XML Simple and Tokeparser troubles
by PodMaster
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |