Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: NOT_FOUND_ERR in insertAfter()

by choroba (Bishop)
on Sep 25, 2013 at 08:53 UTC ( #1055637=note: print w/replies, xml ) Need Help??

in reply to NOT_FOUND_ERR in insertAfter()

NetWallah already told you what was the reason of the error. I would like to add a piece of advice:

Do not go over the elements in a loop (unless you want to add a student to almost each of them). Use XPath to search for the desired target:

#!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $doc = 'XML::LibXML'->load_xml( string => << '__XML__'); <?xml version="1.0" encoding="utf-8" ?> <University> <students> <student id="1000"/> <student id="1001"/> <student id="1002"/> </students> </University> __XML__ my $query = '//student[@id="1001"]'; for my $ele ($doc->findnodes($query)){ my $new_ele = $doc->createElement('student'); $new_ele->setAttribute('id', '1003'); $ele->parentNode->insertAfter($new_ele, $ele) or die; last; # Not needed if identifiers are unique. The whole + looping is useless, then. } print $doc->toString;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055637]
erix wonders... how is the neurocent doing these days?
[marto]: Invest in my crypto currency, derpcoin

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-05-25 15:48 GMT
Find Nodes?
    Voting Booth?