Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

XML serialization with attributes handling

by KSURi (Monk)
on Jan 30, 2009 at 17:54 UTC ( [id://740254]=perlquestion: print w/replies, xml ) Need Help??

KSURi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Monks

I'm writing a module which sends XML queries to specific service. So I've already written stupid serialization method (using XML::LibXML) which takes perl variables and returns XML.
According to service's protocol specification there are some complex tags in queries which contains attributes.
Please suggest any ideas of how can I add attributes handling to my serialization method.
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 || '') } } }

Usage example:
$self->_xml_serialize( $doc_root, outer => { inner => 'value' } )
Example output:
...<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
    I personally prefer a templating mechanism over any XML-module when it comes to generating XML from data-structures. If it's good for HTML, it's good for XML too. See Creating huge files with the Template Toolkit.


    holli

    When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.
Re: XML serialization with attributes handling
by Your Mother (Archbishop) on Jan 30, 2009 at 23:09 UTC

    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.)

      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>...
Re: XML serialization with attributes handling
by runrig (Abbot) on Jan 30, 2009 at 18:23 UTC
    It seems like you could do this a lot more simply with XML::Simple. If the default output is not good enough for your purposes (and it probably isn't), then you can tweak the output with the options.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://740254]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found