in reply to XML serialization with attributes handling

The difference between element/nodes and attributes is somewhat arbitrary in some cases so it would really depend on the service spec how to do it. If you have an XML example with attributes from them and perhaps a Perl data structure you think should match, I'm sure someone will be able to help you. (Me, later, if you put something to that effect up.)

  • Comment on Re: XML serialization with attributes handling

Replies are listed 'Best First'.
Re^2: XML serialization with attributes handling
by KSURi (Monk) on Jan 31, 2009 at 08:47 UTC
    Thanks for the replay
    I've already solved my problem. Dunno why I haven't done it earlier... The code is simple stupid again, but seems working:
    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) } } } }
    Now I can pass attributes with '-' prefix:
    $qiwi->_xml_create(outer => {'-outer-attr' => 'val', '+' => 'outer tex +t'})

    Output:
    ...<outer outer-attr="val">outer text</outer>...