http://www.perlmonks.org?node_id=1144846

bhushanQA has asked for the wisdom of the Perl Monks concerning the following question:

I am having below xml output,

$VAR1 = { 'cdr' => [ { 'networkInfo' => { 'extPayer' => '50351851444', 'eventSource' => 'J@nus', 'sequenceNumber' => '0', 'connectionId' => '0050351851444 +', 'payer' => '50351851444', 'status' => '1', 'mvnoId' => '2', 'currentStatus' => '2', 'eventReferenceNumber' => '15101 +41545000003101', 'sessionId' => '1510143920000000 +011', 'errorCode' => '0', 'previousStatus' => '1', 'eventDateTime' => '201510141545 +33', 'requestType' => '51', 'actionDateTime' => '20151014154 +533', 'exponent' => '-6' }, 'payerInfo' => { 'newPayerStatus' => '2', 'noOfPayerValidity' => '0', 'prevPayerStatus' => '1', 'noOfWalletsUsed' => '0', 'baseWalletBalance' => '0' }, 'pricedInfo' => { 'totalUsedUnits' => '0', 'taxingOptionUsed' => '0', 'totalSalePrice' => '0', 'totalTax' => '0', 'totalCostPrice' => '0', 'chargeSummary' => { 'dimensionSumm +ary' => { + 'dimensionId' => '0', + 'componentSummary' => { + 'totalSalePrice' => '0', + 'totalCostPrice' => '0', + 'componentId' => '0', + 'totalDiscountAmount' => '0', + 'bucketsUsed' => '0' + } + } }, 'totalDiscountAmount' => '0', 'bucketsUsed' => '0' } }, { 'networkInfo' => { 'extPayer' => '50351851444', 'eventSource' => 'J@nus', 'sequenceNumber' => '0', 'connectionId' => '0050351851444 +', 'payer' => '50351851444', 'status' => '1', 'mvnoId' => '2', 'currentStatus' => '2', 'eventReferenceNumber' => '15101 +41545000003101', 'sessionId' => '1510143920000000 +021', 'errorCode' => '0', 'previousStatus' => '2', 'eventDateTime' => '201510141545 +35', 'requestType' => '53', 'actionDateTime' => '20151014154 +535', 'exponent' => '-6' }, 'payerInfo' => { 'newPayerStatus' => '2', 'noOfPayerValidity' => '0', 'prevPayerStatus' => '2', 'noOfWalletsUsed' => '0', 'baseWalletBalance' => '0' }, 'pricedInfo' => { 'totalUsedUnits' => '0', 'taxingOptionUsed' => '0', 'totalSalePrice' => '0', 'totalTax' => '0', 'totalCostPrice' => '0', 'chargeSummary' => { 'dimensionSumm +ary' => { + 'dimensionId' => '0', + 'componentSummary' => { + 'totalSalePrice' => '0', + 'totalCostPrice' => '0', + 'componentId' => '0', + 'totalDiscountAmount' => '0', + 'bucketsUsed' => '0' + } + } }, 'totalDiscountAmount' => '0', 'bucketsUsed' => '0' } }, { 'networkInfo' => { 'extPayer' => '50351851444', 'eventSource' => 'J@nus', 'sequenceNumber' => '0', 'connectionId' => '0', 'payer' => '50351851444', 'status' => '1', 'mvnoId' => '2', 'currentStatus' => '2', 'eventReferenceNumber' => '0', 'sessionId' => '2015101400000335 +940', 'errorCode' => '0', 'previousStatus' => '2', 'eventDateTime' => '201510141545 +40', 'requestType' => '3', 'actionDateTime' => '20151014154 +535', 'exponent' => '-6', 'forceDebit' => '0' }, 'payerInfo' => { 'newPayerStatus' => '2', 'noOfPayerValidity' => '0', 'prevPayerStatus' => '2', 'noOfWalletsUsed' => '0', 'baseWalletBalance' => '9998000000 +' }, 'pricedInfo' => { 'totalUsedUnits' => '0', 'taxingOptionUsed' => '0', 'totalSalePrice' => '0', 'totalTax' => '0', 'totalCostPrice' => '0', 'chargeSummary' => { 'dimensionSumm +ary' => { + 'dimensionId' => '0', + 'componentSummary' => { + 'totalSalePrice' => '0', + 'totalCostPrice' => '0', + 'componentId' => '0', + 'totalDiscountAmount' => '0', + 'bucketsUsed' => '0' + } + } }, 'totalDiscountAmount' => '0', 'bucketsUsed' => '0' } }, { 'networkInfo' => { 'extPayer' => '50351851444', 'instanceId' => '0', 'imei' => '0', 'passwordId' => '0', 'networkUsageIdentifier' => '670 +60224951', 'ReleaseCause' => '01144', 'spendControlId' => '0', 'defaultPayer' => '50351851444', 'sessionId' => '1510141545000003 +101', 'chargingGroupNumberDiscountPerc +ent' => '0', 'errorCode' => '0', 'calledPartyAddress' => '0050399 +051801', 'callingPartyAddress' => '005035 +1851444', 'semiAutoCollectClassId' => '0', 'destinationZoneCode' => '1021', 'requestType' => '1001', 'redirectingPartyId' => '', 'ApplicationServer' => '70602617 +71', 'chargingGroupDiscountPercent' = +> '0', 'timeZone' => '+1Q', 'chargingGroupClass' => '0', 'ccRequestType' => '3', 'defaultExtPayer' => '5035185144 +4', 'carrierCode' => '', 'PrefixDialled' => '', 'dialingType' => '99', 'connectionType' => '0', 'mcc' => '706', 'locationInfo' => '2495100001', 'originalCalledPartyId' => '', 'specialNumberClassId' => '0', 'eventDateTime' => '201510141545 +35', 'bearerService' => '0', 'smscRefernceNumber' => '', 'sequenceNumber' => '0', 'redirectionInformation' => '', 'unitType' => '0', 'connectTime' => '20151014154535 +', 'holidayCode' => '0', 'imsi' => '51851444', 'serviceIdentifier' => '0', 'ratingZoneType' => '1', 'mscAddress' => '', 'enterpriseCall' => '0', 'isTollFree' => '0', 'specialLocationClassId' => '0', 'routingNumber' => '', 'eventType' => '0', 'payerType' => '0', 'connectionId' => '0050351851444 +', 'payer' => '50351851444', 'subscriberClass' => '0', 'chargingGroupId' => '0', 'callReferenceNumber' => '303130 +3032303031', 'mvnoId' => '2', 'originatingNetworkId' => '1021' +, 'callType' => '1', 'dialDigitId' => '1041', 'actionDateTime' => '20151014154 +535', 'callTerminationCause' => '1', 'vpnGroupId' => '0', 'mnc' => '02', 'exponent' => '-6' }, 'payerInfo' => { 'newPayerStatus' => '2', 'noOfPayerValidity' => '0', 'prevPayerStatus' => '2', 'walletDetails' => { 'walletId' => ' +1', 'prevWalletBala +nce' => '10000000000', 'prevExpiryDate +' => '20991231', 'expiryFlag' => + '0', 'newWalletBalan +ce' => '9998000000', 'newExpiryDate' + => '20991231' }, 'noOfWalletsUsed' => '1', 'baseWalletBalance' => '9998000000 +' }, 'pricedInfo' => { 'totalUsedUnits' => '120', 'totalCostPrice' => '2000000', 'bucket' => { 'rateId' => '1002', 'discountPercentage' +=> '0', 'startUnits' => '1', 'exchangeRate' => '10 +00000', 'taxFormulaId' => '10 +02', 'componentId' => '0', 'taxWalletId' => '1', 'currencyRateId' => ' +1', 'discountArticleId' = +> '1001', 'walletId' => '1', 'salePrice' => '20000 +00', 'discountAmount' => ' +0', 'TaxDetail' => { 'Tax' +=> { + 'amount' => '0', + 'count' => '1', + 'article' => '1009' + } }, 'usedUnits' => '120', 'endUnits' => '120', 'taxApplied' => '0', 'debitedValue' => '20 +00000', 'costPrice' => '20000 +00', 'dimensionId' => '0', 'serviceKey' => '0', 'chargeArticleId' => +'1', 'tariffId' => '1026' }, 'taxingOptionUsed' => '0', 'totalTax' => '0', 'totalSalePrice' => '2000000', 'chargeSummary' => { 'dimensionSumm +ary' => { + 'dimensionId' => '0', + 'componentSummary' => { + 'totalSalePrice' => '2000000', + 'totalCostPrice' => '2000000', + 'componentId' => '0', + 'totalDiscountAmount' => '0', + 'bucketsUsed' => '1' + } + } }, 'bucketsUsed' => '1', 'totalDiscountAmount' => '0' } } ] };

I want to retrieve "requestType" from every 'networkInfo' parameter, we have 3 to 4 network info parameter, I want to retrieve only "requestType"= 1001, How Can I achieve this ? I am using xml:simple and I want to stick with that only.. pls help.
  • Comment on Perl : How to retrieve from xml when xml file having same attribute multiple time
  • Download Code

Replies are listed 'Best First'.
Re: Perl : How to retrieve from xml when xml file having same attribute multiple time
by Skeeve (Parson) on Oct 14, 2015 at 12:16 UTC

    If I understood correctly, you might want to look into the man page of XML::Simple. Especially at "ForceArray".


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Perl : How to retrieve from xml when xml file having same attribute multiple time
by poj (Abbot) on Oct 14, 2015 at 12:32 UTC
    for my $cdr (@{$VAR1->{'cdr'}}){ my $info = $cdr->{'networkInfo'}; if ($info->{'requestType'} eq '1001'){ print $info->{'sessionId'}."\n"; # for example } }
    poj
      Hi Pijo, Thanks for your help and helping me to understand it clearly. As you have already looked into my question and understand it better way.. Can I ask you one more question ?? with your help I am able to retrieve the values from network info, but if I want to retrieve the values from 'payerInfo' and 'pricedInfo' based on the requestType 1001 from that particular section how can I proceed ?, as we can see there are so many sections .. and I don't want to deal with other only requestType 1001 and followed by 'payerInfo' and 'pricedInfo' of the same 'networkInfo' Thanks in Advance..!!
        for my $cdr (@{$VAR1->{'cdr'}}){ my $network = $cdr->{'networkInfo'}; if ($network->{'requestType'} eq '1001'){ my $payer = $cdr->{'payerInfo'}; my $priced = $cdr->{'pricedInfo'}; print $network->{'sessionId'}."\n"; print $payer->{'baseWalletBalance'}."\n"; print $priced->{'totalCostPrice'}."\n"; } }
        poj
      Thanks Poj its working perfectly.. what a good solution :)
Re: Perl : How to retrieve from xml when xml file having same attribute multiple time
by u65 (Chaplain) on Oct 14, 2015 at 12:03 UTC

    You will get help faster if you show what you have tried so far, i.e., please show your code you have tried.

Re: Perl : How to retrieve from xml when xml file having same attribute multiple time
by sundialsvc4 (Abbot) on Oct 14, 2015 at 12:33 UTC

    Even though you say that you “want to stick with Simple,” XML::LibXML (which is a binding for libxml2) will make your task far more “simple,” even trivial.   XPath expressions are the industry-standard way to look for things in an XML document, and they work beautifully in Perl.   Concerns for the exact structure of the document are removed from your Perl source-code and put into ... a string.   Support for anything you might want to do with an XML document is right here.

    Furthermore, this library is the most widely-used tool (in any language) for both accessing and creating XML documents.   So, you are probably accessing the file with the same software that was used to create it.