Mongers,
 
Anyone use XML::LibXML? 
 
Its "Element" and "Node" classes have methods for
1. Reading the value of nodes
2. Appending data to existing data
3. And setting attributes,
 
but apparently NO method for replacing existing data in a node. 
 
When you need to replace existing data the best method we have been able to find is creating an unbound node, appending the data in it, and replacing the original node with the new one (by finding its parent and using the replaceChild method.)  Ugh!   20 lines of high maintenance code where there should be one:
 
            my $new_node;
            my $parentDoc = $nodes[0]->ownerDocument;
            my @atts = $nodes[0]->attributes if $nodes[0]->hasAttributes;
            eval { $new_node = $parentDoc->createElement($nodes[0]->nodeName) };
            if(@atts) {
                for(@atts) {
                    my $name = $_->name;
                    my $value = $_->value;
                    $new_node->setAttribute($name,$value);
                }
            }
            die "Could not create new node: $@" if $@;
 
            $new_node->appendText('sometext');  
            my $parent;
            eval {
                $parent = $nodes[0]->parentNode;
                $parent->replaceChild($new_node,$nodes[0]);
            };
            die "Could not replace node: $@"  if $@;
 
 I've heard it's the fault of the underlying C code in libxml2, but regardless, it seems as though someone in the chain has missed a mighty big ball.
 
I'm hoping that one of the mongers knows the solution and that I'm the one that's missed the ball.  Comments, suggestions, etc. most welcome.
 
Jim Eshelman 
 

Reply via email to