Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
i m very new to perl and programming...pls help me....
i have an XML which is below mentioned:

HTML
<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>


i have given space after brackets else it is getting converted...
and my code is as below:

PERL
#!/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("fm.xml");


my $booklist = XMLin('fm.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";
    }



my output of my program is as follows:
133752115,34390939283,133753116,,500110,US,6,0,
133754115,34390939284,133755116,,,,,,


problem is i m not able to fetch all the tag values which are repeating in the same node....can anyone help me in this aspect or guide me...

Regards
Amar
Posted
Updated 25-Apr-12 2:42am
v4
Comments
Nelek 19-Apr-12 17:48pm    
Edit: Changed code tag to perl

It's been a while since I did xml with perl. When I learned to do it, we did something like this:


PERL
#!/usr/bin/perl

#use xml parser
use XML::Parser;

#set up the parser obj
$xp = new XML::Parser();
$xp->setHandlers(Start=> \&start,End=> \&end, Char => \&cdata);

#get some use variables
$currentTag = "";
$currentData = "";

#get the file
$xp->parsefile("YOURFILENAME.xml");

#this is called when a start tag is found
sub start()
{
    my($parser,$name,%attr) = @_;
    $currentTag = $name;
}

#this is called when data is found
sub cdata()
{
    my($parser,$data) = @_;

   #use logic here to do different things with tags
   if($currentTag eq 'what i was looking for')
    {
       $currentData .= $data;
    }
   elseif($currentTag eq 'something else i might use')
    {
       #do something else with data
    }
}


#this is called when end of tag is found
sub end()
{
    my($parser,$name) = @_;

    if($name eq 'something i was looking for')
     {
        #output because your tag closed. 

        #reset
       $currentData = "";
     }

    $currentTag = "";
}




I find that slightly easier to keep track of , but that is probably just preference. Most of the time I was picking through rss feeds. To me it seemed to run in logical order, and it lets you go tag by tag.
 
Share this answer
 
v2
Comments
Nelek 19-Apr-12 17:49pm    
Edit: Changed code tags to perl
loctrice 19-Apr-12 18:19pm    
yeah, thanks. I didn't see it as an option.
Nelek 19-Apr-12 19:20pm    
Don't worry, you are not blind. It is not in the drop-down menu, but it is accepted as tag if you type it manually
Amar Venkat Jammula 24-Apr-12 9:02am    
i did't get you...can you pls ask me in detail...
i m not able to follow... can you pls give me specific code...
 
Share this answer
 
Comments
loctrice 20-Apr-12 16:57pm    
I gave example code. What specifically are you having trouble following? If you point it out, I'll be happy to expand.
Amar Venkat Jammula 24-Apr-12 9:01am    
problem is i m not able to fetch all the tag values which are repeating in the same node...please refer my xml that i have pasted above....
loctrice 24-Apr-12 9:44am    
That is why I posted the solution I did. You attach a function to each opening tag, closing tag, and data. It will execute the firs any time an opening tag is found, the data when it reads between, and the last whenever a closing is found. Play around with it a little bit, it will give you the control you are looking for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900