Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML

Status
Not open for further replies.

rpack78

Programmer
Oct 1, 2004
35
US
I am trying to parse an XML file using XML::Simple. Everything is working fine except when the node has no value. When I do this:

Code:
print my $car_vin = $record->{LoanUniversal}->[$i]->{Loan}->{LoanInformation}->{CollateralID};

I get "HASH(0xa1fc9d8)". How can I test if $car_vin has this HASH/hex crap in it so I can blank it out? Or should I go about it another way? I'm not quite understanding what's going on. Thanks.
 
What you are seeing is a hash reference. CollateralID points to another hash, so you need to lengthen it to[tt]
$record->{LoanUniversal}->[$i]->{Loan}
->{LoanInformation}->{CollateralID}->{something};[/tt]
 
HASH/hex crap you are refering to indicates that the value is a pointer to another HASH. It seems that you can drill down even further into this. Try this

my $car_vin = $record->{LoanUniversal}->[$i]->{Loan}->{LoanInformation}->{CollateralID};

if ($car_vin =~ /^HASH(/)
{
#POINTER - we can drill down further
while (my($key,$val) = each (%$car_vin))
{ print "$key -> $val\n"; }
}
else
{
#it's just a value
print "$car_vin\n";
}
 
Thanks for the response, guys. It makes sense what you are saying. I tried the code you suggested and it entered the "if" part but printed nothing. Here is what the XML looks like up to the node in question.

Code:
<LoanApps>
 <LoanUniversal>
  <Loan Counter="275317" RecordCounter="1042222">
   <LoanInformation>
    <ApplicationNumber>0194260</ApplicationNumber> 
    <ApplicationDate>1/6/2005</ApplicationDate> 
    <LoanType>DEL NORTE CU</LoanType> 
    <LoanPurpose>PURCHASE</LoanPurpose> 
    <LoanNumber /> 
    <RequestedLoanAmount>17570.0000</RequestedLoanAmount> 
    <RequestedTerm>60</RequestedTerm> 
    <RequestedRate>5.3500</RequestedRate> 
    <CollateralID /> 
    <CollateralType>USED AUTO</CollateralType> 
    ...

Notice how CollateralID doesn't have a value? It doesn't have any child nodes either. I guess I could just use the regex you supplied and blank out the value if "HASH(" is found. I just want to know why the HASH/hex crap is printing.
 
You can use the SuppressEmpty option to tell the parser what to do with empty elements like CollateralID. The possible values are:
1 Skips empty elements
'' Produces an empty string
undef Produces an undefined value

for example:[tt]
$xml=XMLin("loanapps.xml",SuppressEmpty=>1);[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top