Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: NOT_FOUND_ERR in insertAfter()

by choroba (Canon)
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 having an uproarious good time at the Monastery: (5)
As of 2015-07-30 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls