manunamu has asked for the wisdom of the Perl Monks concerning the following question:
Monks:
Salutations! Here's my question.
I have an xml file which is as follows:
<?xml version="1.0" encoding="UTF-8" ?> <CustomerInvoice> <XSDVersion>1.5.7</XSDVersion> <RecordType></RecordType> <OutFileName></OutFileName> <DateTime></DateTime> <InvoiceDetails> <CompanyInfo> <CompanyName>XXX Ltd</CompanyName> <RegistrationID>REG</RegistrationID> <TaxInvoiceGSTRegistrationNo>DDDD</TaxInvoiceGSTRegi +strationNo> <Comment>Timbuctu</Comment> <TermsAndCondition></TermsAndCondition> </CompanyInfo> <InvHeader> <CustId>000</CustId> <CustCode>1111</CustCode> <CuInvRefNum>abc</CuInvRefNum> <CuInternalRefNum></CuInternalRefNum> <CuOriginalInvoiceNo></CuOriginalInvoiceNo> <CuInvoiceDate>XXX</CuInvoiceDate> <CuBillStartDate>XXX</CuBillStartDate> <CuBillEndDate>XXX</CuBillEndDate> <CuTotalDeposit></CuTotalDeposit> <CuNonTaxableAmount>0.00</CuNonTaxableAmount> <CuTotalTax>5.05</CuTotalTax> <CuTaxArray> <CuTaxDescription>YYY</CuTaxDescriptio +n> <CuTaxPercentage>7.0000</CuTaxPercenta +ge> <CuTaxableAmount>72.1168</CuTaxableAmo +unt> <CuTaxAmount>5.05</CuTaxAmount> </CuTaxArray> <CuTotalAdjustments></CuTotalAdjustments> <CuInvoiceAmount>77.17</CuInvoiceAmount> <CuInvoiceCurrency>XXX</CuInvoiceCurrency> <CuTaxableAmountBase></CuTaxableAmountBase> <CuTotalTaxBase></CuTotalTaxBase> <CuBaseCurrency>XXX</CuBaseCurrency> <CuPreviousBalance></CuPreviousBalance> <CuPaymentAmount></CuPaymentAmount> <CuBalance>77.17</CuBalance> <CuDueDate>XXXX</CuDueDate> <CuOverdueAmount></CuOverdueAmount> <CuPaymentMode>Cash</CuPaymentMode> <CuPaymentTerm>Payment received on or after YYY</CuP +aymentTerm> <CuDeliveryNo></CuDeliveryNo> <CuDODate></CuDODate> <CuDeliveryDate></CuDeliveryDate> <CuPOReference></CuPOReference> <CustomerInfoField1>WWWW</CustomerInfoField1> <CustomerInfoField2>ZZZZ</CustomerInfoField2> <CustomerInfoField3></CustomerInfoField3> <CustomerInfoField4></CustomerInfoField4> </InvHeader> <AddressInfo> <AddressLine1></AddressLine1> <AddressLine2>MR SAMPLE_CUST</AddressLine2> <AddressLine3> GROSS AVENUE 1</AddressLine3> <AddressLine4>#9999-9999</AddressLine4> <AddressLine5>Timbuctu XXXX</AddressLine5> <AddressLine6></AddressLine6> </AddressInfo> <ExecSummary> <SummaryDesc>Market Summary</SummaryDesc> <DivCostFlatTotalCharges>72.1168</DivCostFlatTotalCh +arges> <SubscriberLevel> <SubscriberName>MR SAMPLE_CUST_</Subsc +riberName> <Market> <Name>XXX</Name> <OneTimeCharge>10.00 +00</OneTimeCharge> <RecCharge>42.0968</ +RecCharge> <UsageCharge>20.0200 +</UsageCharge> <OCCCharge>0.0000</O +CCCharge> <SubsidyCharge></Sub +sidyCharge> <TotalDiscounts>0.00 +00</TotalDiscounts> <TotChargeBeforeTax> +72.1168</TotChargeBeforeTax> </Market> </SubscriberLevel> </ExecSummary> <ServiceInfo> <CoServiceLabel>0</CoServiceLabel> <CoPackageDescription>GGG</CoPackageDescription> <CoServiceNumber>999999</CoServiceNumber> <CoServiceAddress>MR SAMPLE_CUST</CoServiceAddress> <CoServiceName></CoServiceName> <CoTotOccCharges></CoTotOccCharges> <CoServiceTotal>72.1168</CoServiceTotal> <ServiceInfoField1></ServiceInfoField1> <ServiceInfoField2></ServiceInfoField2> <ServiceInfoField3></ServiceInfoField3> <ServiceInfoField4></ServiceInfoField4> <ServiceInfoField5>XXX</ServiceInfoField5> <ServiceInfoField6></ServiceInfoField6> <ServiceInfoField7>GGG</ServiceInfoField7> <ChargeType> <ServicesD +etails> <SDPackageName></SDP +ackageName> <SDTaxIndicator>G</S +DTaxIndicator> <SDServiceName>GGG80 +0</SDServiceName> <SDQuantity></SDQuan +tity> <SDPrice></SDPrice> <UOM></UOM> <SDChargedAmount>10. +0000</SDChargedAmount> <Comment></Comment> </Services +Details> <SDChargeType>GGG</SDChargeType> <SDChargeDesc></SDChargeDesc> <SDChargeTypeSubTotal>10.0000</SDCharg +eTypeSubTotal> </ChargeType> <ChargeType> <ServicesD +etails> <SDPackageName></SDP +ackageName> <SDTaxIndicator>G</S +DTaxIndicator> <SDServiceName>GGG80 +0</SDServiceName> <SDQuantity></SDQuan +tity> <SDPrice></SDPrice> <UOM></UOM> <SDChargedAmount>13. +0968</SDChargedAmount> <Comment></Comment> + <SDChargedPeriod> + <ChargeStartDate>9999-10-01</ChargeStartDate> + <ChargeEndDate>9999-10-14</ChargeEndDate> + </SDChargedPeriod> + </ServicesDetails> <ServicesD +etails> <SDPackageName></SDP +ackageName> <SDTaxIndicator>G</S +DTaxIndicator> <SDServiceName>GGG80 +0</SDServiceName> <SDQuantity></SDQuan +tity> <SDPrice></SDPrice> <UOM></UOM> <SDChargedAmount>29. +0000</SDChargedAmount> <Comment></Comment> + <SDChargedPeriod> + <ChargeStartDate>9999-10-15</ChargeStartDate> + <ChargeEndDate>9999-11-14</ChargeEndDate> + </SDChargedPeriod> + </ServicesDetails> <SDChargeType>Charges</SDChargeType> <SDChargeDesc></SDChargeDesc> <SDChargeTypeSubTotal>42.0968</SDCharg +eTypeSubTotal> </ChargeType> <ChargeType> <ServicesD +etails> <SDPackageName>purch +ase1</SDPackageName> <SDTaxIndicator>G</S +DTaxIndicator> <SDServiceName>purch +ase1</SDServiceName> <SDChannelSelection> +</SDChannelSelection> <SDQuantity></SDQuan +tity> <SDInstalmentCounter +></SDInstalmentCounter> <SDPrice></SDPrice> <UOM></UOM> <SDChargedAmount>10. +020</SDChargedAmount> <Comment></Comment> </Services +Details> <ServicesD +etails> <SDPackageName>Purch +ase2</SDPackageName> <SDTaxIndicator>G</S +DTaxIndicator> <SDServiceName>Purch +ase2</SDServiceName> <SDChannelSelection> +</SDChannelSelection> <SDQuantity></SDQuan +tity> <SDInstalmentCounter +></SDInstalmentCounter> <SDPrice></SDPrice> <UOM></UOM> <SDChargedAmount>10. +000</SDChargedAmount> <Comment></Comment> + <SDUsageDetails> + <UsageTaxIndicator>G</UsageTaxIndicator> + <UsageDesc_2>1633</UsageDesc_2> + <UsageDesc_1>XXX XXX</UsageDesc_1> + <UsageDesc>purchase1</UsageDesc> + <PrimQuantity>2</PrimQuantity> + <PrimUOM>Unit</PrimUOM> + <SecQuantity></SecQuantity> + <SecUOM></SecUOM> + <TerTairyQuantity></TerTairyQuantity> + <TerTairyUOM></TerTairyUOM> + <ChargedAmount>10.0200</ChargedAmount> + </SDUsageDetails> + <SDUsageDetails> + <UsageTaxIndicator>G</UsageTaxIndicator> + <UsageDesc_2>1633</UsageDesc_2> + <UsageDesc_1>XXX XXX</UsageDesc_1> + <UsageDesc>Purchase2</UsageDesc> + <PrimQuantity>1</PrimQuantity> + <PrimUOM>Unit(s)</PrimUOM> + <SecQuantity></SecQuantity> + <SecUOM></SecUOM> + <TerTairyQuantity></TerTairyQuantity> + <TerTairyUOM></TerTairyUOM> + <ChargedAmount>10.0000</ChargedAmount> + </SDUsageDetails> + </ServicesDetails> <SDChargeType>Charges</SDChargeType> <SDChargeDesc>9999-01-01 to 9999-01-31 +</SDChargeDesc> <SDChargeTypeSubTotal>20.0200</SDCharg +eTypeSubTotal> </ChargeType> <CallGroupInfo> <CallGroupDescription>purchase1</CallG +roupDescription> <CallGroupTotalAmount></CallGroupTotal +Amount> <CallGroupRemarks></CallGroupRemarks> </CallGroupInfo> <CallGroupInfo> <CallGroupDescription>purchase1</CallG +roupDescription> <CallGroupTotalAmount></CallGroupTotal +Amount> <CallGroupRemarks></CallGroupRemarks> <CallDetai +lByService> <CallDetai +lByService> + <CallDetailSubTotals> + <Description>Sub-Total for Purchase Details</Description> + <Amount>20.0200</Amount> + </CallDetailSubTotals> + <CallDetailInfo> + <TaxIndicator>G</TaxIndicator> + <RoamingPartner></RoamingPartner> + <Country></Country> + <Date>9999-10-04</Date> + <Time>17:23:41</Time> + <TelNumber></TelNumber> + <Duration1></Duration1> + <Unit1></Unit1> + <Duration2></Duration2> + <Unit2></Unit2> + <Amount>10.0000</Amount> + </CallDetailInfo> + <CallDetailInfo> + <TaxIndicator>G</TaxIndicator> + <RoamingPartner></RoamingPartner> + <Country></Country> + <Date>9999-10-04</Date> + <Time>18:00:30</Time> + <TelNumber></TelNumber> + <Duration1></Duration1> + <Unit1></Unit1> + <Duration2></Duration2> + <Unit2></Unit2> + <Amount>0.0200</Amount> + </CallDetailInfo> + <CallDetailInfo> + <TaxIndicator>G</TaxIndicator> + <RoamingPartner></RoamingPartner> + <Country></Country> + <Date>9999-10-02</Date> + <Time>10:24:43</Time> + <TelNumber></TelNumber> + <Duration1></Duration1> + <Unit1></Unit1> + <Duration2></Duration2> + <Unit2></Unit2> + <Amount>10.0000</Amount> + </CallDetailInfo> <Description></Descr +iption> <TelNumber></TelNumb +er> <Amount></Amount> <Description></Descr +iption> <TelNumber></TelNumb +er> <Amount></Amount> </CallDeta +ilByService> </CallGroupInfo> </ServiceInfo> </InvoiceDetails> <NumInvoice>1</NumInvoice> <TotAmount>77.17</TotAmount> <TotCust>1</TotCust> </CustomerInvoice>
The perl program I am running is as follows:
use strict; use warnings; use XML::Parser; use Data::Dumper; use List::Util qw(max); my $file_name; my @file_name1; my $fp1; my $xml_string; my $pkg_txt; my $got_ebook=0; my $call_grp_info = 0; my $CallDetailByService_cntr=0; my $output_file_fp; sub handle_end_xcd { my ( $expat, $element, %attrs ) = @_; my $line = $expat->current_line; my @parent_item_array=$expat->context; my $parent_tag=$parent_item_array[-1]; print $output_file_fp " IN END Element Found $element\n"; if ($element eq 'CallGroupInfo') { if ($got_ebook == 1) { print STDOUT "$xml_string\n"; } } else { print STDOUT "$xml_string\n"; } } sub handle_start_xcd { my ( $expat, $element, %attrs ) = @_; my $line = $expat->current_line; my @parent_item_array=$expat->context; my $parent_tag=$parent_item_array[-1]; if ( ($element eq 'CallGroupInfo') && ($got_ebook > 1 ) ) { $call_grp_info++; print $output_file_fp "in IF $call_grp_info\n"; if ($call_grp_info == 1) { print STDOUT "$xml_string\n"; } } elsif ($element eq "CallDetailByService") { $CallDetailByService_cntr++; if ($CallDetailByService_cntr > 1) { print STDOUT "$xml_string\n"; } } elsif ($element eq "CallGroupDescription") { print STDERR "XXX\n"; } else { print STDOUT "$xml_string\n"; } } sub char_handler { my ($p, $data) = @_; print $output_file_fp "Char handler", $p->current_element, "\n"; if ($p->current_element eq "SDPackageName") { chomp $data; # print STDERR "Package Data is:$data\n"; $got_ebook++ if ( ($data eq "eMagazine Purchase") || ($data eq + "eBook Purchase") ) ; print STDOUT "$xml_string\n"; } else { print STDOUT "$xml_string\n"; } } # End char_handler @file_name1=glob("org1.xml"); my $output_file_name="test.xml"; open ($output_file_fp,">", $output_file_name); my $p1 = XML::Parser->new ( Handlers => { Start => \&handle_start_xcd, Char => \&char_handler, End => \&handle_end_xcd } ) ; foreach (@file_name1) { $file_name = $_; chomp $file_name; open($fp1,"<",$file_name); while(<$fp1>) { chomp; $xml_string=$_; #print $output_file_fp "$xml_string"; chomp $xml_string; if ( grep ( /SDPackageName/, $xml_string) || grep ( /Cal +lGroupInfo/ ,$xml_string) || grep ( /CallDetailByService/ ,$xml_strin +g ) || grep ( /CallGroupDescription/ ,$xml_string )) { $p1->parse($xml_string); } else { print STDOUT "$xml_string\n"; } } }
when I run the program, it gives me an error which is as follows:
no element found at line 1, column 51, byte 51 at D:/Dwimperl/perl/ven +dor/lib/XML/Parser.pm line 187
Have I encountered a bug in XML::PARSER?
Appreciate all help!
Thanks, Manu
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bug in XML::Parser (maxims)
by davido (Cardinal) on Oct 22, 2013 at 16:49 UTC | |
by manunamu (Initiate) on Oct 23, 2013 at 04:08 UTC | |
by davido (Cardinal) on Oct 23, 2013 at 04:13 UTC | |
by manunamu (Initiate) on Oct 23, 2013 at 05:20 UTC | |
by marinersk (Priest) on Oct 23, 2013 at 12:48 UTC | |
|
Re: Bug in XML::Parser
by McA (Priest) on Oct 22, 2013 at 16:22 UTC | |
|
Re: Bug in XML::Parser
by aaron_baugher (Curate) on Oct 22, 2013 at 12:53 UTC | |
|
Re: Bug in XML::Parser
by zork42 (Monk) on Oct 22, 2013 at 13:27 UTC | |
|
Re: Bug in XML::Parser
by graff (Chancellor) on Oct 23, 2013 at 04:24 UTC | |
by manunamu (Initiate) on Oct 23, 2013 at 05:03 UTC | |
by graff (Chancellor) on Oct 23, 2013 at 06:01 UTC | |
by manunamu (Initiate) on Oct 23, 2013 at 11:04 UTC | |
by graff (Chancellor) on Oct 23, 2013 at 21:03 UTC | |
|
Re: Bug in XML::Parser ( my file )
by Anonymous Monk on Oct 22, 2013 at 11:48 UTC | |
|
Re: Bug in XML::Parser
by McA (Priest) on Oct 22, 2013 at 11:50 UTC | |
|
Re: Bug in XML::Parser
by graff (Chancellor) on Oct 23, 2013 at 04:43 UTC |