How to convert xml to csv ?

I am in need of converting billions of XML into csv file to load data to DB, i have found the below code in perl but not sure why it's not working properly.

CODE:

#!/usr/bin/perl

# Script to illustrate how to parse a simple XML file
# and pick out all the values for a specific element, in
# this case all the titles.

# use strict;
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple (KeyAttr=>[]);

# read XML file
$data = $xml->XMLin("test1.xml");


my $booklist = XMLin('test1.xml');    #booklist is the array 
# print Dumper($booklist);

foreach my $FreemanFees (@{$booklist->{FreemanFees}}) {


    print 
    $FreemanFees->{SdcLoanFacilityNumber} , "," , 
    $FreemanFees->{DealId} ,",", 
    $FreemanFees->{Tranche}->{SdcDealNumber} , "," , 
    $FreemanFees->{Tranche}->{ManagerFeeAndCredits}->{ManagerFeeAndCredit}->{FreemanFeesForManager}->{ManagerNumberForFreemanFee}, ",",
    $FreemanFees->{Tranche}->{ManagerFeeAndCredits}->{ManagerFeeAndCredit}->{FreemanFeesForManager}->{currencyId},",",
    $FreemanFees->{Tranche}->{ManagerFeeAndCredits}->{ManagerFeeAndCredit}->{FreemanFeesForManager}->{sdcCurrencyCode} , "," , 
    $FreemanFees->{Tranche}->{ManagerFeeAndCredits}->{ManagerFeeAndCredit}->{FreemanFeesForManager}->{scale}, ",",
    $FreemanFees->{Tranche}->{ManagerFeeAndCredits}->{ManagerFeeAndCredit}->{FreemanFeesForManager}->{content} , "," ,"\n" ;
    }

Input:

<body>
    <FreemanFees>
        <SdcLoanFacilityNumber>133752115</SdcLoanFacilityNumber>
        <DealId>34390939283</DealId>
        <Tranche>
            <SdcDealNumber>133753116</SdcDealNumber>
            <ManagerFeeAndCredits>
                <ManagerFeeAndCredit>
                    <FreemanFeesForManager scale="6" currencyId="500110" sdcCurrencyCode="US">0</FreemanFeesForManager>
                    <ManagerNumberForFreemanFee>8320</ManagerNumberForFreemanFee>
                </ManagerFeeAndCredit>
            </ManagerFeeAndCredits>
        </Tranche>
    </FreemanFees>
    <FreemanFees>
        <SdcLoanFacilityNumber>133754115</SdcLoanFacilityNumber>
        <DealId>34390939284</DealId>
        <Tranche>
            <SdcDealNumber>133755116</SdcDealNumber>
            <ManagerFeeAndCredits>
                <ManagerFeeAndCredit>
                    <FreemanFeesForManager scale="6" currencyId="500110" sdcCurrencyCode="US">0</FreemanFeesForManager>
                    <ManagerNumberForFreemanFee>9678</ManagerNumberForFreemanFee>
                </ManagerFeeAndCredit>
                <ManagerFeeAndCredit>
                    <FreemanFeesForManager scale="6" currencyId="500110" sdcCurrencyCode="US">0</FreemanFeesForManager>
                    <ManagerNumberForFreemanFee>5390</ManagerNumberForFreemanFee>
                </ManagerFeeAndCredit>
            </ManagerFeeAndCredits>
        </Tranche>
    </FreemanFees>
</body>

The code is not printing the second Child tag.

Can any one help me ?

When you run your code it says pretty clearly why it can't print the details for the second child node:

Not a HASH reference at ...

Now, modify your code to print the contents of $FreemanFees using Data::Dumper at the beginning of each loop run, then compare the output for the first child node and the second child node. Do you see the difference? (TIP: look at the line containing "ManagerFeeAndCredit" key).

PS: "use strict;" should always be enabled.