in reply to XML::Simple functionality with XML::Twig ?
use XML::Twig; # reading routines ; same way as xml::simple; it puts everything in a +easy to use arrayref. my $xmldata = XML::Twig->new->parsefile('test.xml')->simplify( keyattr + => []); # writing routines ; same way as xml::simple; # write the array to xml code and of'course get the indented for free +;) my $twig = XML::Twig->new(pretty_print => 'indented', empty_tags = +> 'html'); my $elt = create_element(xml => $xmldata); undef($xmldata); $twig->set_root($elt); $twig->print(); # this output undef($twig); sub create_element { my $gi = shift; my $data = shift; my $t = XML::Twig::Elt->new($gi); if (ref $data) { while (my ($k,$v) = each(%$data)) { if ($k ne "outline") { # if the field is called "outline" it will be automatically put to # CDATA else it will return normal escaped data. create_element($k, $v)->paste(last_child => $t); } else { $t->insert_new_elt( last_child => $k => { '#CDATA' => 1 }, + $v); } } } else { $t->set_text($data); } $t; }
|
---|