in reply to Re: XML serialization with attributes handling
in thread XML serialization with attributes handling
Now I can pass attributes with '-' prefix: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 { if($k =~ s/^-//o) { $parent_node->setAttribute($k, $v) } # + just one line of code for handling attributes elsif($k =~ s/^\+//o) { $parent_node->appendText($v) } # a +nd one for text childs else { $parent_node->appendTextChild($k, $v) } } } }
$qiwi->_xml_create(outer => {'-outer-attr' => 'val', '+' => 'outer tex +t'})
...<outer outer-attr="val">outer text</outer>...
|
|---|