[prog] perl XML::LibXML
Caroline Johnston
johnston at biochemistry.ucl.ac.uk
Tue Feb 15 00:49:44 EST 2005
Hi guys,
I've just started playing around with XML::LibXML and I think I'm missing
something fairly fundamental. I'm trying to create a <div> and insert it
into an XHTML tree. Can anyone tell me what I'm doing wrong or suggest a
tutorial, the perldocs left me baffled.
#make new node
$div_node = XML::LibXML::Element->new('div');
$div_node->setAttribute('id',$params->{id});
#pull out the reference nodes based on id's given as args
my @before_node=$body->findnodes('//*[@id="'."$params->{before}".'"]')
if $params->{before};
my @after_node=$body->findnodes('//*[@id="'."$params->{after}".'"]')
if $params->{after};
#if we've been told where to put it, put it there
if (@after_node)
{
$body->insertAfter($div_node, $after_node[0]);
}
elsif (@before_node)
{
$body->insertBefore($div_node, $before_node[0]);
}
#otherwise, stick it at the end of the <body>
else
{
$body->appendChild($div_node);
}
The appendChild call works fine, but the insertBefore and insertAfter
don't. I don't get any errors, it just doesn't insert anything. As far as
I can tell div_node, body, before_node and after_node contain what I think
they should contain.
If I change it to something like
$body->insertBefore($div_node, $body->firstChild)
then the new div gets inserted after the first child of body. Why does
this work but not if I use a ref node?
What am I missing?
Many thanks!
Cxx
More information about the Programming
mailing list