Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: NOT_FOUND_ERR in insertAfter()

by boftx (Deacon)
on Sep 25, 2013 at 05:25 UTC ( #1055607=note: print w/ replies, xml ) Need Help??


in reply to Re: NOT_FOUND_ERR in insertAfter()
in thread NOT_FOUND_ERR in insertAfter()

Update

I tested the change above, and found that it does not correct the problem, you need to use the form I have below with undef as the second arg to insertAfter. It is possible that this is a result of different versions, so test for yourself.

End update

I was just going to post the same thing as above after a quick test script. Here is the output produced by making the change:

<?xml version="1.0" encoding="utf-8"?> <University> <students> <student id="1000"/> <student id="1001"><student id="1003"/></student> <student id="1002"/> </students> </University>

On a different note, you should always have use strict; and use warnings; as well as check for critical failures in your code, like this:

#!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $parser = XML::LibXML->new(); # this call should probably be wrapped in an eval (or Try::Tiny) or at + least # tested for a valid object being returned. my $doc = $parser->parse_file('student.xml'); my $query = '//student'; for my $ele ($doc->findnodes($query)){ my $attr_text=$ele->getAttribute('id'); if($attr_text eq '1001'){ my $new_ele=$doc->createElement('student'); $new_ele->setAttribute('id','1003'); $ele->insertAfter($new_ele,undef); last; } } # if you can't open the file you want to know why open(TESTFILE,">result.xml") or die "Can not open output: $!"; print TESTFILE $doc->toString; close(TESTFILE); exit; __END__
On time, cheap, compliant with final specs. Pick two.


Comment on Re^2: NOT_FOUND_ERR in insertAfter()
Select or Download Code
Re^3: NOT_FOUND_ERR in insertAfter()
by NetWallah (Abbot) on Sep 25, 2013 at 05:34 UTC
    Sorry - but your code is incorrect.

    The main problem is that the output XML has the 1003 student EMBEDDED INSIDE the 1001, which I do not think is the desired result.

    my code produces:

    <?xml version="1.0" encoding="utf-8"?> <University> <students> <student id="1000"/> <student id="1001"/><student id="1003"/> <student id="1002"/> </students> </University>
    which , I believe is what the OP wanted.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

      I agree that your output is more desirable. I suspect that there is some version difference accounting for what we are seeing. I used your code and got the same error that the OP reported, even though, like you, I know his error was caused by using $doc instead of $ele. That is why I suggested that the OP try both your code and mine.

      Hmmm, would have helped if I had logged in before replying to you. :)

      On time, cheap, compliant with final specs. Pick two.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-21 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (106 votes), past polls