taralon has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
Let me preface my issue with some introductory information. I have recently taken over the creation and management of tools written in Perl at my job. Until six months ago my Perl coding was minimal, the learning curve has been a bit steep, but I have found a way around all my issues to date by reading posts on this, and other sites. Most of my coding background comes from college classes that are now, unfortunately, 16+ years in the past.
I have now run into an issue that I just can't seem to wrap my head around.
We have log files at work that contain returns from outside machines in XML format. I could parse the returns using combinations of split and regular expressions to get what I need out of the XML but I have been trying to parse the XML with XML::Simple (tried using XML::LibXML as well but I can't get it to return a valid data structure so I have put that on a back burner again). However, when I try to step through the data structure returned by XML::Simple, I get nowhere. I am obviously missing something, but I haven't been able to find out what by reading posts and such online. I hope that someone will point out where and why I am going wrong.
Below is a test version of the code I am using for the XML Portion of the code. Everything up to this point works as I expect it to, so I will just post that. Typically I would be running a "foreach my $xmlfile (@logfileperformancelist)" to lead into this code. Forgive me as the code blocks are going to be long as I have included some test data directly in the code as I was checking to see how this works, and the data structure given by Data::Dumper below that as well.
use strict; use warnings; use XML::Simple; use Data::Dumper; use File::Slurp; ## test script to verify the way xml is parsed## ## $xmlfile is set to data of a format that would be expected to b +e passed to this if used in a loop ## my $xmlfile = '<?xml version="1.0" encoding="UTF-8"?><TMSMessage x +mlns="http://xmlns.sony.net/d-cinema/tms-api/v1"><MessageHeader><Id>- +1</Id><Type>ListPerformances</Type><Timestamp>1433796462559</Timestam +p><Source>TMS</Source></MessageHeader><MessageBody><ListPerformancesR +esponse><EventInfoList><EventInfo><EventId>2015060400000157</EventId> +<EventTitle>zClosing Shutdown 3D v6 - New</EventTitle><SplId>20100101 +00000038</SplId><SplTitle>zClosing Shutdown 3D v6 - New</SplTitle><Au +ditoriumId>15</AuditoriumId><AuditoriumNo>15</AuditoriumNo><EventStar +tTime>2015-06-08T00:30:00-07:00</EventStartTime><DcpFlg>true</DcpFlg> +<KdmFlg>true</KdmFlg><EventDuration>464</EventDuration><ShowStartTime +>2015-06-08T00:30:00-07:00</ShowStartTime><ShowDuration>464</ShowDura +tion><SpokenLang></SpokenLang><SubtitleLang></SubtitleLang><Rating></ +Rating><OverlappedEventFlg>false</OverlappedEventFlg><UncompletedShow +Flg>false</UncompletedShowFlg><HavingShowFlg>true</HavingShowFlg><Eve +ntBodyColor>696969</EventBodyColor><ImportFlg>false</ImportFlg><Overl +appedNum>1</OverlappedNum><HeightPos>0</HeightPos><WaitGPIFlg>false</ +WaitGPIFlg><EventEndTime>2015-06-08T00:37:44-07:00</EventEndTime><Thr +eeDimension>true</ThreeDimension><EventStatus>0</EventStatus><Importe +dShowId></ImportedShowId><ImportedAnalogFlg>false</ImportedAnalogFlg> +<EventRating></EventRating><RatingFlg>true</RatingFlg><PreshowPackLis +t></PreshowPackList></EventInfo><EventInfo><EventId>2015060400000164< +/EventId><EventTitle>zMorning Startup 3D v3</EventTitle><SplId>201106 +1100000013</SplId><SplTitle>zMorning Startup 3D v3</SplTitle><Auditor +iumId>15</AuditoriumId><AuditoriumNo>15</AuditoriumNo><EventStartTime +>2015-06-08T12:10:00-07:00</EventStartTime><DcpFlg>true</DcpFlg><KdmF +lg>true</KdmFlg><EventDuration>10</EventDuration><ShowStartTime>2015- +06-08T12:10:00-07:00</ShowStartTime><ShowDuration>10</ShowDuration><S +pokenLang></SpokenLang><SubtitleLang></SubtitleLang><Rating></Rating> +<OverlappedEventFlg>false</OverlappedEventFlg><UncompletedShowFlg>fal +se</UncompletedShowFlg><HavingShowFlg>true</HavingShowFlg><EventBodyC +olor>696969</EventBodyColor><ImportFlg>false</ImportFlg><OverlappedNu +m>1</OverlappedNum><HeightPos>0</HeightPos><WaitGPIFlg>false</WaitGPI +Flg><EventEndTime>2015-06-08T12:10:10-07:00</EventEndTime><ThreeDimen +sion>true</ThreeDimension><EventStatus>0</EventStatus><ImportedShowId +></ImportedShowId><ImportedAnalogFlg>false</ImportedAnalogFlg><EventR +ating></EventRating><RatingFlg>true</RatingFlg><PreshowPackList></Pre +showPackList></EventInfo><EventInfo><EventId>2015060200000562</EventI +d><EventTitle>* Movie1 3D-AV</EventTitle><SplId>2015060800000048</Spl +Id><SplTitle>w23_3DP1_Sanandreas_SCCAP_0421pre_150605_02222_TMS_</Spl +Title><AuditoriumId>15</AuditoriumId><AuditoriumNo>15</AuditoriumNo>< +EventStartTime>2015-06-08T12:55:00-07:00</EventStartTime><DcpFlg>true +</DcpFlg><KdmFlg>true</KdmFlg><EventDuration>8195</EventDuration><Sho +wStartTime>2015-06-08T12:29:18-07:00</ShowStartTime><ShowDuration>973 +7</ShowDuration><SpokenLang>en,en,en,en,en,en,en,en,en,en</SpokenLang +><SubtitleLang></SubtitleLang><Rating>US-G US-G US-G US-G + US-G US-G US-G US-G US-G US-G + </Rating><OverlappedEventFlg>false</OverlappedEventFlg><Uncomple +tedShowFlg>false</UncompletedShowFlg><HavingShowFlg>true</HavingShowF +lg><EventBodyColor>696969</EventBodyColor><ImportFlg>true</ImportFlg> +<OverlappedNum>1</OverlappedNum><HeightPos>0</HeightPos><WaitGPIFlg>f +alse</WaitGPIFlg><EventEndTime>2015-06-08T15:11:35-07:00</EventEndTim +e><ThreeDimension>true</ThreeDimension><EventStatus>0</EventStatus><I +mportedShowId>00343929</ImportedShowId><ImportedAnalogFlg>false</Impo +rtedAnalogFlg><EventRating>US-13</EventRating><RatingFlg>true</Rating +Flg><PreshowPackList><PreshowPack><PackId>b863a576-d8a3-4f5d-92a7-b66 +f876f6d7c</PackId><FrameName>Preshow</FrameName><FrameNumber>1</Frame +Number></PreshowPack><PreshowPack><NoAssign>true</NoAssign><FrameName +>3D_Advertisement</FrameName><FrameNumber>1</FrameNumber></PreshowPac +k><PreshowPack><PackId>a55164d0-faa7-4c7c-a688-ed36011add95</PackId>< +FrameName>Postshow</FrameName><FrameNumber>1</FrameNumber></PreshowPa +ck></PreshowPackList></EventInfo><EventInfo><EventId>2015060200000563 +</EventId><EventTitle>* Movie1 3D-AV</EventTitle><SplId>2015060600000 +017</SplId><SplTitle>w23_3DP1_Sanandreas_SCCAP_0421pre_150605_02062_T +MS_</SplTitle><AuditoriumId>15</AuditoriumId><AuditoriumNo>15</Audito +riumNo><EventStartTime>2015-06-08T16:05:00-07:00</EventStartTime><Dcp +Flg>true</DcpFlg><KdmFlg>true</KdmFlg><EventDuration>8195</EventDurat +ion><ShowStartTime>2015-06-08T15:42:13-07:00</ShowStartTime><ShowDura +tion>9562</ShowDuration><SpokenLang>en,en,en,en,en,en,en,en,en,en</Sp +okenLang><SubtitleLang></SubtitleLang><Rating>US-G US-G US-G + US-G US-G US-G US-G US-G US-G US-G + </Rating><OverlappedEventFlg>false</OverlappedEventFlg><Unc +ompletedShowFlg>false</UncompletedShowFlg><HavingShowFlg>true</Having +ShowFlg><EventBodyColor>FFFFFF</EventBodyColor><ImportFlg>true</Impor +tFlg><OverlappedNum>1</OverlappedNum><HeightPos>0</HeightPos><WaitGPI +Flg>false</WaitGPIFlg><EventEndTime>2015-06-08T18:21:35-07:00</EventE +ndTime><ThreeDimension>true</ThreeDimension><EventStatus>0</EventStat +us><ImportedShowId>00343929</ImportedShowId><ImportedAnalogFlg>false< +/ImportedAnalogFlg><EventRating>US-13</EventRating><RatingFlg>true</R +atingFlg><PreshowPackList><PreshowPack><PackId>760eb073-81f8-45cb-855 +3-484c5cecb740</PackId><FrameName>Preshow</FrameName><FrameNumber>1</ +FrameNumber></PreshowPack><PreshowPack><NoAssign>true</NoAssign><Fram +eName>3D_Advertisement</FrameName><FrameNumber>1</FrameNumber></Presh +owPack><PreshowPack><PackId>a55164d0-faa7-4c7c-a688-ed36011add95</Pac +kId><FrameName>Postshow</FrameName><FrameNumber>1</FrameNumber></Pres +howPack></PreshowPackList></EventInfo></EventInfoList></ListPerforman +cesResponse></MessageBody></TMSMessage>'; my $performancelist=XMLin($xmlfile, forcearray => 1, KeyAttr => {EventInfo => 'EventId'} ## +#ventID is a unique value that I want to use as a key value ## ); write_file 'mydump.log', Dumper($performancelist); ## this is where nothing happens ## foreach my $performance (@{$performancelist->{MessageBody}->{ListP +erformancesResponse}->{EventInfoList}->{EventInfo}->{EventId}}) { my $output=$performance->{PreshowPackList}->{PreshowPack}->{Pa +ckId}; print "Debug: $output.\n"; }
Which errors out with:
"Not a HASH reference at xmlparse.pl line 15"
Looking at the output of Dumper I found that my keyattr isn't being keyed on at all. If I remove the forcearray=>1 from the above code, then Dumper does output what I expect, but it doesn't get me any closer to running correctly.
I believe that the problem I am running into is the fact that I only get one $var returned and I am trying to step through this as if an array. I thought that I could treat the key attribute that I referenced in the code as an array and step through that by referencing to it.
The output of Dumper is below:
$VAR1 = { 'xmlns' => 'http://xmlns.sony.net/d-cinema/tms-api/v1', 'MessageBody' => [ { 'ListPerformancesResponse' => [ { 'EventInf +oList' => [ + { + 'EventInfo' => [ + { + 'EventEndTime' => [ + '2015-06-08T00:37:44-0 +7:00' + ], + 'DcpFlg' => [ + 'true' + ], + 'EventRating' => [ + {} + ], + 'OverlappedEventFlg' => [ + 'false' + ], + 'ImportedShowId' => [ + {} + ], + 'ThreeDimension' => [ + 'true' + ], + 'EventDuration' => [ + '464' + ], + 'RatingFlg' => [ + 'true' + ], + 'ImportedAnalogFlg' => [ + 'false' + ], + 'AuditoriumNo' => [ + '15' + ], + 'SpokenLang' => [ + {} + ], + 'SplId' => [ + '2010010100000038' + ], + 'HavingShowFlg' => [ + 'true' + ], + 'UncompletedShowFlg' => [ + 'false' + ], + 'ShowStartTime' => [ + '2015-06-08T00:30:00- +07:00' + ], + 'ShowDuration' => [ + '464' + ], + 'EventBodyColor' => [ + '696969' + ], + 'EventStartTime' => [ + '2015-06-08T00:30:00 +-07:00' + ], + 'ImportFlg' => [ + 'false' + ], + 'EventTitle' => [ + 'zClosing Shutdown 3D v6 + - New' + ], + 'Rating' => [ + {} + ], + 'AuditoriumId' => [ + '15' + ], + 'WaitGPIFlg' => [ + 'false' + ], + 'HeightPos' => [ + '0' + ], + 'PreshowPackList' => [ + {} + ], + 'EventId' => [ + '2015060400000157' + ], + 'EventStatus' => [ + '0' + ], + 'SplTitle' => [ + 'zClosing Shutdown 3D v6 - + New' + ], + 'OverlappedNum' => [ + '1' + ], + 'SubtitleLang' => [ + {} + ], + 'KdmFlg' => [ + 'true' + ] + }, + { + 'EventEndTime' => [ + '2015-06-08T12:10:10-0 +7:00' + ], + 'DcpFlg' => [ + 'true' + ], + 'EventRating' => [ + {} + ], + 'OverlappedEventFlg' => [ + 'false' + ], + 'ImportedShowId' => [ + {} + ], + 'ThreeDimension' => [ + 'true' + ], + 'EventDuration' => [ + '10' + ], + 'RatingFlg' => [ + 'true' + ], + 'ImportedAnalogFlg' => [ + 'false' + ], + 'AuditoriumNo' => [ + '15' + ], + 'SpokenLang' => [ + {} + ], + 'SplId' => [ + '2011061100000013' + ], + 'HavingShowFlg' => [ + 'true' + ], + 'UncompletedShowFlg' => [ + 'false' + ], + 'ShowStartTime' => [ + '2015-06-08T12:10:00- +07:00' + ], + 'ShowDuration' => [ + '10' + ], + 'EventBodyColor' => [ + '696969' + ], + 'EventStartTime' => [ + '2015-06-08T12:10:00 +-07:00' + ], + 'ImportFlg' => [ + 'false' + ], + 'EventTitle' => [ + 'zMorning Startup 3D v3' + ], + 'Rating' => [ + {} + ], + 'AuditoriumId' => [ + '15' + ], + 'WaitGPIFlg' => [ + 'false' + ], + 'HeightPos' => [ + '0' + ], + 'PreshowPackList' => [ + {} + ], + 'EventId' => [ + '2015060400000164' + ], + 'EventStatus' => [ + '0' + ], + 'SplTitle' => [ + 'zMorning Startup 3D v3' + ], + 'OverlappedNum' => [ + '1' + ], + 'SubtitleLang' => [ + {} + ], + 'KdmFlg' => [ + 'true' + ] + }, + { + 'EventEndTime' => [ + '2015-06-08T15:11:35-0 +7:00' + ], + 'DcpFlg' => [ + 'true' + ], + 'EventRating' => [ + 'US-13' + ], + 'OverlappedEventFlg' => [ + 'false' + ], + 'ImportedShowId' => [ + '00343929' + ], + 'ThreeDimension' => [ + 'true' + ], + 'EventDuration' => [ + '8195' + ], + 'RatingFlg' => [ + 'true' + ], + 'ImportedAnalogFlg' => [ + 'false' + ], + 'AuditoriumNo' => [ + '15' + ], + 'SpokenLang' => [ + 'en,en,en,en,en,en,en,en +,en,en' + ], + 'SplId' => [ + '2015060800000048' + ], + 'HavingShowFlg' => [ + 'true' + ], + 'UncompletedShowFlg' => [ + 'false' + ], + 'ShowStartTime' => [ + '2015-06-08T12:29:18- +07:00' + ], + 'ShowDuration' => [ + '9737' + ], + 'EventBodyColor' => [ + '696969' + ], + 'EventStartTime' => [ + '2015-06-08T12:55:00 +-07:00' + ], + 'ImportFlg' => [ + 'true' + ], + 'EventTitle' => [ + '* Movie1 3D-AV' + ], + 'Rating' => [ + 'US-G US-G US-G U +S-G US-G US-G US-G US-G US-G US-G + ' + ], + 'AuditoriumId' => [ + '15' + ], + 'WaitGPIFlg' => [ + 'false' + ], + 'HeightPos' => [ + '0' + ], + 'PreshowPackList' => [ + { + 'PreshowPack' => +[ + +{ + + 'FrameName' => [ + + 'Preshow' + + ], + + 'FrameNumber' => [ + + '1' + + ], + + 'PackId' => [ + + 'b863a576-d8a3-4f5d-92a7-b66f876f6d7c' + + ] + +}, + +{ + + 'NoAssign' => [ + + 'true' + + ], + + 'FrameName' => [ + + '3D_Advertisement' + + ], + + 'FrameNumber' => [ + + '1' + + ] + +}, + +{ + + 'FrameName' => [ + + 'Postshow' + + ], + + 'FrameNumber' => [ + + '1' + + ], + + 'PackId' => [ + + 'a55164d0-faa7-4c7c-a688-ed36011add95' + + ] + +} + ] + } + ], + 'EventId' => [ + '2015060200000562' + ], + 'EventStatus' => [ + '0' + ], + 'SplTitle' => [ + 'w23_3DP1_Sanandreas_SCCAP +_0421pre_150605_02222_TMS_' + ], + 'OverlappedNum' => [ + '1' + ], + 'SubtitleLang' => [ + {} + ], + 'KdmFlg' => [ + 'true' + ] + }, + { + 'EventEndTime' => [ + '2015-06-08T18:21:35-0 +7:00' + ], + 'DcpFlg' => [ + 'true' + ], + 'EventRating' => [ + 'US-13' + ], + 'OverlappedEventFlg' => [ + 'false' + ], + 'ImportedShowId' => [ + '00343929' + ], + 'ThreeDimension' => [ + 'true' + ], + 'EventDuration' => [ + '8195' + ], + 'RatingFlg' => [ + 'true' + ], + 'ImportedAnalogFlg' => [ + 'false' + ], + 'AuditoriumNo' => [ + '15' + ], + 'SpokenLang' => [ + 'en,en,en,en,en,en,en,en +,en,en' + ], + 'SplId' => [ + '2015060600000017' + ], + 'HavingShowFlg' => [ + 'true' + ], + 'UncompletedShowFlg' => [ + 'false' + ], + 'ShowStartTime' => [ + '2015-06-08T15:42:13- +07:00' + ], + 'ShowDuration' => [ + '9562' + ], + 'EventBodyColor' => [ + 'FFFFFF' + ], + 'EventStartTime' => [ + '2015-06-08T16:05:00 +-07:00' + ], + 'ImportFlg' => [ + 'true' + ], + 'EventTitle' => [ + '* Movie1 3D-AV' + ], + 'Rating' => [ + 'US-G US-G US-G US +-G US-G US-G US-G US-G US-G US-G + ' + ], + 'AuditoriumId' => [ + '15' + ], + 'WaitGPIFlg' => [ + 'false' + ], + 'HeightPos' => [ + '0' + ], + 'PreshowPackList' => [ + { + 'PreshowPack' => +[ + +{ + + 'FrameName' => [ + + 'Preshow' + + ], + + 'FrameNumber' => [ + + '1' + + ], + + 'PackId' => [ + + '760eb073-81f8-45cb-8553-484c5cecb740' + + ] + +}, + +{ + + 'NoAssign' => [ + + 'true' + + ], + + 'FrameName' => [ + + '3D_Advertisement' + + ], + + 'FrameNumber' => [ + + '1' + + ] + +}, + +{ + + 'FrameName' => [ + + 'Postshow' + + ], + + 'FrameNumber' => [ + + '1' + + ], + + 'PackId' => [ + + 'a55164d0-faa7-4c7c-a688-ed36011add95' + + ] + +} + ] + } + ], + 'EventId' => [ + '2015060200000563' + ],
Update: <
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML Parsing Problems
by tangent (Parson) on Jun 08, 2015 at 23:43 UTC | |
by taralon (Initiate) on Jun 09, 2015 at 18:28 UTC | |
|
Re: XML Parsing Problems
by Anonymous Monk on Jun 08, 2015 at 22:48 UTC | |
by Discipulus (Canon) on Jun 09, 2015 at 07:21 UTC | |
by Jenda (Abbot) on Oct 13, 2015 at 11:25 UTC | |
|
Re: XML Parsing Problems
by thanos1983 (Parson) on Jun 09, 2015 at 00:48 UTC |