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

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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2015-10-06 21:01 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (160 votes), past polls