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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re^2: NOT_FOUND_ERR in insertAfter() by boftx
in thread NOT_FOUND_ERR in insertAfter() by kingsaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found