Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: NOT_FOUND_ERR in insertAfter()

by choroba (Abbot)
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;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: NOT_FOUND_ERR in insertAfter()
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-12-20 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (96 votes), past polls