Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

XML::Twig Eating Up Data

by anshumangoyal (Scribe)
on Nov 03, 2012 at 13:23 UTC ( #1002103=perlquestion: print w/replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I have a script which reads an excel file to change some parameters. This is my code:
#!/usr/bin/perl -w use strict; use XML::Twig; use Spreadsheet::XLSX; my $twig = XML::Twig->new; $twig->parsefile('ue_profile.xml'); my $workbook = Spreadsheet::XLSX->new('FSL_XML.xlsx'); for my $worksheet ( $workbook->worksheets() ) { my $worksheet_name = $worksheet->{Name}; my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min + 1 .. $row_max ) { my @para_array; for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->{Cells}[$row][$col]; my $cellValue = $cell->{Val}; if ($cellValue !~ /NA/i) { push (@para_array, $cellValue); } } my $value = pop(@para_array); my $Search_Key = join('/', @para_array); $Search_Key = "/".$Search_Key."/"; if ($Search_Key =~ m/cqiformatIndPeriodicSubbandK/i) { print "Hello"; } my (@model) = $twig->findnodes($Search_Key); foreach (@model) { $_->set_text($value); } } }
The output XML Eats up some data: This is my Original XML:
<createUe> <!-- 0 = None , 1 = phyCqiReportingPeriodic , +2 = phyCqiReportingAperiodic, 3= Both --> <cqiReportingMode>0</cqiReportingMode> <!-- 0 to 4 --> <phyCqiReportingAperiodic>1</phyCqiReportingAp +eriodic> <!-- -1 to 6 --> <nomPdschRsEpreOffset>0 </nomPdschRsEpreOffset +> <phyCqiReportingPeriodic> <!-- 0 = release , 1 = setup --> <cqiReportingPeriodicMode>0</cqiReportingP +eriodicMode> <!-- 0 to 1185 --> <cqiPucchResourceIndex>0 </cqiPucchResourc +eIndex> <!-- 0 to 1023 --> <cqiPmiConfigIndex> 25 </cqiPmiConfigIndex +> <!-- 0 = wideBand 1 = subBand --> <cqiSubBandWideBandChoice>1</cqiSubBandWid +eBandChoice> <!-- Optional --> <!-- 1 to 4 --> <cqiformatIndPeriodicSubbandK>3</cqiformat +IndPeriodicSubbandK> <!-- Optional --> <!-- 0 to 1023 --> <riConfigIndex>483 </riConfigIndex> <!-- 0 or 1 --> <simultaneousAckNackAndCQI>0 </simultaneou +sAckNackAndCQI> </phyCqiReportingPeriodic> </createUe>
and it changes to this:
<createUe> <!-- 0 = None , 1 = phyCqiReportingPeriodic , 2 = phyCqiReport +ingAperiodic, 3= Both --> <cqiReportingMode>1187</cqiReportingMode> <!-- 0 to 4 --> <phyCqiReportingAperiodic>1189</phyCqiReportingAperiodic> <!-- -1 to 6 --> <nomPdschRsEpreOffset>1188</nomPdschRsEpreOffset> <phyCqiReportingPeriodic>1196</phyCqiReportingPeriodic> </createUe>
Dont know why this is happening. Can some one help. Let me know if some other information is required.

Replies are listed 'Best First'.
Re: XML::Twig Eating Up Data
by Don Coyote (Pilgrim) on Nov 03, 2012 at 19:35 UTC

    Hi anshumangoyal,

    I have had minimal experience with xml, however, do any of these questions help locate the issue;

    • Given the match for 'NA' is case insensitive,
      • Are all the rows of different column lengths?
      • Are the 'NA's entered by hand?
      • Could a different approach to the match help, such as matching for m/^[cqi|nom|sim|etc...]/?
    • What kind of value is $Searchkey?
      • Does $Searchkey require the final "/" to match the xpath element?
      • Is $Searchkey a regex? would it need to be compiled as "qr/".$Searchkey."/"?
      • if so would be beter to compile with a) single quotes and b) non path delimiters eg 'qr#'.$Searchkey.'#'
      • have you tried printing out the $Searchkey values to see what is being matched?

    I hope these prompts can help towards debugging.

    My hunch, is that the match cuts off after the first element as you are using the forward slash as your match delimiter. By using a different delimiter for the matching may resolve this because paths are delimited by the forward slashes the same as are used to delimit matches.

      Exactly this was the error. Removed it and it was resolved. Thanks a lot.
Re: XML::Twig Eating Up Data
by mirod (Canon) on Nov 05, 2012 at 12:38 UTC

    Saddly it is impossible to run your example without FSL_XML.xlsx, so I can't be sure of what is going on exactly.

    That said, it looks to me like at some point you are setting the text for the phyCqiReportingPeriodic element, which replaces all of its content. Check for this, figure out why this is happening and you should be on your way to solving the problem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002103]
Approved by Old_Gray_Bear
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-01-19 03:35 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (167 votes). Check out past polls.