Dear Monks

I am parsing an XML file using and the following (stripped down) code:

use strict; use warnings; use XML::LibXML::Reader; my $XMLfile = 'myfile.xml'; print "Parsing...\n"; my $reader = 'XML::LibXML::Reader'->new(location => $XMLfile) or d +ie print "Error"; my $entry_pattern = 'XML::LibXML::Pattern'->new('/martif/text/body +/termEntry'); my $counter=5000; my $signalprint=0; while ($reader->nextPatternMatch($entry_pattern)) { my $ID; my $value=""; my $info=""; my $termEntry = $reader->copyCurrentNode(1); for my $subject ($termEntry->findnodes('descripGrp')) { $info = $subject->findvalue('./descrip'); } for my $lang_set ($termEntry->findnodes('langSet')) { my $ID = $lang_set->getAttribute('xml:lang'); for my $term_grp ($lang_set->findnodes('./tig')){ $value = $term_grp->findvalue('./term'); } } $counter--; #committing to database every $counter executions if ($counter eq 0){ $counter=5000; $signalprint = $signalprint + $counter; print "Done $signalprint\n"; } } print "Finished!\n";

When the script reaches the end, perl crashes after printing Finished! I do not get any error message. It seems data were processed correctly. The problem must reside in this line my $termEntry = $reader->copyCurrentNode(1);, as I get the same error further stripping down the code:

use strict; use warnings; use XML::LibXML::Reader; my $XMLfile = 'myfile.xml'; print "Parsing...\n"; my $reader = 'XML::LibXML::Reader'->new(location => $XMLfile) or d +ie print "Error"; my $entry_pattern = 'XML::LibXML::Pattern'->new('/martif/text/body +/termEntry'); while ($reader->nextPatternMatch($entry_pattern)) { my $termEntry = $reader->copyCurrentNode(1); } print "Finished!\n";

In this case the script goes through the whole XML till printing "Finished!" and crashes. Note that deleting the line my $termEntry = $reader->copyCurrentNode(1); perl does not crash anymore.

Any idea?

I am on Windows 10 using perl 5.16

=================

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE martif SYSTEM "TBXcoreStructV02.dtd"> <martif type="TBX-Default" xml:lang="en"> <martifHeader> <fileDesc> <sourceDesc> </sourceDesc> </fileDesc> <encodingDesc> <p type="XCSURI">TBXXCS.xcs</p> </encodingDesc> </martifHeader> <text> <body> <termEntry id="28836"> <descripGrp> <descrip type="subjectField">04</descrip> <note>economics,</note> </descripGrp> <langSet xml:lang="en"> <tig> <term>employment policy</term> <termNote type="termType">fullForm</termNote> <descrip type="reliabilityCode">3</descrip> </tig> </langSet> <langSet xml:lang="nl"> <tig> <term>werkgelegenheidsbeleid</term> <termNote type="termType">fullForm</termNote> <descrip type="reliabilityCode">3</descrip> </tig> </langSet> </termEntry> </body> </text> </martif>

In reply to Perl panicking XML::LibXML::Reader copyCurrentNode by IB2017

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.