0

I am trying to modify an attribute value in an XML file using LibXML. Here is the part of my xml file

xmlns="urn:x-xxx:r2:reg-doc:1551-fad.110.05:en:*">
<meta-data><?Pub Dtl?>
<confidentiality class="xxx-internal"/>
<doc-name>XXX</doc-name>
<doc-id>
<doc-no type="registration">XXX</doc-no><language
code="en"/><rev>A</rev>
<date><y>2013</y><m>04</m><d>22</d></date>
</doc-id>
<company-id>
<business-unit></business-unit>
<company-name></company-name>
<company-symbol logotype="XX"></company-symbol>
</company-id>
<title>XXX</title>
<drafted-by>
<person>
<name>XXX</name><signature>XXX</signature>
<location>IN</location><company>XXX</company><department>BC</department>
</person>
</drafted-by>
<approved-by approved="yes">
<person>
<name>XXX</name><signature>XXX</signature>
<location>IN</location><company>XXX</company><department>BC</department>
</person>
</approved-by>

Here is the script i tried.

#!usr/bin/env perl -w
use strict;
use warnings;
use XML::LibXML;
use Data::Dumper;
my $filename="/path/filename.xml";
my $parser=XML::LibXML->new();
my $doc=$parser->parse_file($filename);
my $xpath='/meta-data/approved-by/@approved';
print Dumper $doc->findvalue($xpath);
my ($attr) = $doc->findnodes($xpath);
$attr->setValue('Yes');
print Dumper $doc->findvalue($xpath);

When i execute this script, i get an error message like.

C:\Windows\System32>perl C:\path\perl\sample3.pl
Could not create file parser context for file "/C:/path/1_
1543-CRA 119 1364_2.xml": Result too large at C:\path\perl\sample3.pl line 8

I understand that it is not able to parse, but why? My xml file is a huge one. Is it the problem or is it with my script? please guide me what shld i do. Thanks.

Shahul Hameed
  • 181
  • 2
  • 5
  • 16
  • You might need a *streaming* XML parser that doesn't hold a whole DOM in memory. Often, the [`XML::Twig` module](https://metacpan.org/pod/XML::Twig) is used in such cases, but it has a completely different API from `XML::LibXML`. – amon Mar 10 '14 at 15:10
  • 1
    @amon: [XML::LibXML::Reader](http://p3rl.org/XML::LibXML::Reader) is a pull parser with the same API as [XML::LibXML](http://p3rl.org/XML::LibXML). – choroba Mar 10 '14 at 15:19
  • Did you really specify the filename as `/C:/path/1_1543-CRA 119 1364_2.xml` with a leading slash? – nwellnhof Mar 10 '14 at 16:24
  • @amon: I heard about Twig, but i need to install the module. I work on the hub. I need ask the person to copy the Twig tarball, and then download make file, install it, then execute makefile.pl, then install. I thought it is a big task for an admin who wouldnt prefer doing all. They simply say that they couldnt install it. So is there any easy way to install it? – Shahul Hameed Mar 10 '14 at 18:00
  • @choroba brother, do you mean to say i shd try LibXML::Reader ? – Shahul Hameed Mar 10 '14 at 18:02
  • @nwellnhof yes bro. I did. It says it couldnt parse the "file", that means that it got the file. SO the problem with the syntax/way i m doing it – Shahul Hameed Mar 10 '14 at 18:03

2 Answers2

1

If you specified the filename with a leading slash followed by a drive name, try to remove the leading slash. It's possible that the error message from XML::LibXML is misleading and should have been No such file or directory.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
1

Sorry for the thread necromancy but I have just had this exact same symptom. XML::LibXML reported

"Could not create file parser context for file : Result too large

I was testing three files, one suceeded, two did not. the sucessful file was larger than the failing two and on examination I had made an error in the file path of the failing files.

This message should indeed have been something along the lines of "cannot find file"

If you are experiencing the same check your file path carefully.

Ed.