KSURi has asked for the wisdom of the Perl Monks concerning the following question:
sub _xml_serialize { my $self = shift; my($parent_node, $tree) = @_; no warnings 'uninitialized'; while(my($k, $v) = each %$tree) { if(ref $v eq 'HASH') { my $child_node = XML::LibXML::Element->new($k); $self->_xml_serialize($child_node, $v); $parent_node->appendChild($child_node) } elsif(ref $v eq 'ARRAY') { foreach(@$v) { my $child_node = XML::LibXML::Element->new($k); if(ref eq 'HASH' or ref eq 'ARRAY') { $self->_xml_seri +alize($child_node, $_) } else { $child_node->appendText($_ || '') } $parent_node->appendChild($child_node) } } else { $parent_node->appendTextChild($k, $v || '') } } }
Example output:$self->_xml_serialize( $doc_root, outer => { inner => 'value' } )
...<outer><inner>value</inner></outer>...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML serialization with attributes handling
by holli (Abbot) on Jan 30, 2009 at 18:38 UTC | |
|
Re: XML serialization with attributes handling
by Your Mother (Archbishop) on Jan 30, 2009 at 23:09 UTC | |
by KSURi (Monk) on Jan 31, 2009 at 08:47 UTC | |
|
Re: XML serialization with attributes handling
by runrig (Abbot) on Jan 30, 2009 at 18:23 UTC |