in reply to Re: XML::LibXML creating nodes with a string OR a node
in thread XML::LibXML creating nodes with a string OR a node
That misses the point. The calling function does not know, and the called function does not care.
The answer I have come up with is a wrapper function that does what I want XML::LibXML to do:
sub _addNode { my ($parent, $child) = @_; if(ref($child) eq 'XML::LibXML::Element'){ $parent->appendChild($child); }elsif(ref($child) eq ''){ $parent->appendTextNode($child); }else{ die "'_addNode child is type: ".ref($child). " Do not know what to do with that"; } return $parent; }
Is the answer to my original question "yes" or "no"?
To be rule compliant here is the runnable code....
#!/usr/bin/perl -w use strict; use XML::LibXML; # The XML Document my $DOC = XML::LibXML->createDocument( "1.0", "UTF-8" ); # Set the root my $root = $DOC->createElement('owner'); $DOC->setDocumentElement($root); sub createOwner { if(rand() < 0.5){ return "John Smith"; }else{ my $ret = XML::LibXML::Element->new('href'); $ret->appendTextNode('http://johnsmith.com'); return $ret; } } sub _addNode { my ($parent, $child) = @_; if(ref($child) eq 'XML::LibXML::Element'){ $parent->appendChild($child); }elsif(ref($child) eq ''){ $parent->appendTextNode($child); }else{ die "'_addNode child is type: ".ref($child). " Do not know what to do with that"; } return $parent; } my $owner = &createOwner(); $root = &_addNode($root, $owner); print $DOC."\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: XML::LibXML creating nodes with a string OR a node
by Anonymous Monk on Jun 07, 2015 at 23:31 UTC | |
by worik (Sexton) on Jun 08, 2015 at 21:38 UTC | |
|
Re^3: XML::LibXML creating nodes with a string OR a node
by Anonymous Monk on Jun 08, 2015 at 04:50 UTC | |
by worik (Sexton) on Jun 08, 2015 at 21:38 UTC |