• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Perl - Trying to Parse XML File

Hi,

I am trying to "read" an XML file with PERL with a view to converting some of the element names. Unfortunately I am a bit stuck!

This is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ApplicationData>
<ApplicationIDs ClaimID="0000000000002801" InsurerAcceptingBranchID="001" InsurerAcceptingBranchName="Morgan Cole COMP branch 1" InsurerBranchID="001" InsurerBranchName="Morgan Cole COMP branch 1" InsurerID="COMP028" InsurerName="Morgan Cole COMP" MainApplicant="a, a " RepresentativeBranchID="001" RepresentativeBranchName="Morgan Cole branch 1" RepresentativeID="CR00028" RepresentativeName="Morgan Cole" RepresentativeOrgPath="/CR00028"/>
<ClaimDetails SentDate="2010-04-13"/>
<FraudStated/>
<StatementOfTruth SignatoryType="S" RetainedSignedCopy="1"/>
</ApplicationData>
<ClaimAndClaimantDetails>
<ClaimantRepresentative>
<CompanyDetails CompanyName="Morgan Cole" ContactName="a" ContactSurname="a" ReferenceNumber="a" TelephoneNumber="02008918273645">                  <Address AddressType="A" City="City" Country="United Kingdom" County="County" District="District" HouseName="House" HouseNumber="3" Street1="Street"/>               </CompanyDetails>
</ClaimantRepresentative>
<DefendantDetails DefendantStatus="P" PolicyNumberReference="a">
<PersonalDetails Surname="a" TitleType="1">      <Address AddressType="A" City="a" Country="a" County="a" District="a" HouseName="a" HouseNumber="1" PostCode="a" Street1="a" Street2="a"/>    </PersonalDetails>
<Vehicle VRN="a"/>
<InsurerInformation InsurerName="Morgan Cole" InsurerOrganizationID="COMP028" InsurerOrganizationPath="/COMP028" InsurerType="I">      <BranchDetails BranchID="001" BranchName="Morgan Cole COMP branch 1" BranchPath="/COMP028/001"/>    </InsurerInformation>
<CompanyDetails>
<Address AddressType="A"/>
</CompanyDetails>
</DefendantDetails>
<ClaimantDetails ChildClaim="0" NationalInsuranceNumber="NH814365B" Occupation="a">    <PersonalDetails DateOfBirth="1956-06-16" Name="a" Surname="a" TitleType="1">      <Address AddressType="A" City="a" Country="a" County="a" District="a" HouseName="q" HouseNumber="11" PostCode="a" Street1="a" Street2="a"/>    </PersonalDetails>    <Vehicle VRN="a"/>  </ClaimantDetails>
</ClaimAndClaimantDetails>
<MedicalDetails>
<Injury BoneInjury="0" HospitalAttendance="0" InjurySustainedDescription="v" MedicalAttentionSeeking="0" Other="0" SoftTissue="0" TimeOffRequired="0" Whiplash="1"/>
<Rehabilitation RehabilitationNeeds="0" RehabilitationUndertaken="1"/>
<HospitalSearch/>
</MedicalDetails>
<RepairsAndAlternativeVehicleProvision>
<Repairs ClaiimingDamageOwnVehicle="0">
<AlternativeCompany/>
</Repairs>
<AlternativeVehicleProvision ClaimantEntitled="0">
<Provider>
<Vehicle/>
</Provider>
</AlternativeVehicleProvision>
</RepairsAndAlternativeVehicleProvision>
<AccidentData>
<AccidentDetails AccidentDate="2010-03-01" AccidentDescription="v" AccidentLocation="here" AccidentTime="16:17" ClaimantType="0" OccupantsNumber="1" PoliceReported="0" Seatbelt="1">    <Driver>
<Address/>
</Driver>    <Owner>
<Address/>
</Owner>    <Vehicle/>    <InsuranceCompanyInformatiion>      <Address AddressType="A"/>    </InsuranceCompanyInformatiion>    <WeatherConditions Fog="0" Ice="0" Other="0" Rain="0" Snow="0" Sun="1"/>    <RoadConditions Dry="1" Ice="0" Mud="0" Oil="0" Other="0" Snow="0" Wet="0"/>    <AccidentCircumstances AccidCarPark="0" AccidChangingLanes="0" AccidRoundabout="0" ConcertinaCollision="1" Other="0" VhclHitInRear="0" VhclHitSideRoad="0" VhclHitWhilstParked="0"/>    <PoliceDetails/>  </AccidentDetails>
<BusCoach BussOrCoach="0"/>
</AccidentData>
<OtherPartyDetails/>
<LiabilityFunding>
<Liability DefendantResponsability="v"/>
<Funding FundingUndertaken="0"/>
</LiabilityFunding>
<InsurerResponse>
<LiabilityCausation>    <DefendantAdmits/>  </LiabilityCausation>
<ProvidedServices NotificationDate="2010-04-13">    <DefendantsInsurer>      <Address/>    </DefendantsInsurer>  </ProvidedServices>
</InsurerResponse>
<InterimSettlementPack>
<ClaimantRepresentative PaymentMoreRequested="">
<CompanyDetails CompanyName="Morgan Cole" ContactName="a" ContactSurname="a" ReferenceNumber="a" TelephoneNumber="02008918273645">
<Address AddressType="A" City="City" Country="United Kingdom" HouseNumber="3" Street1="Street"/>
</CompanyDetails>
</ClaimantRepresentative>
<DefendantRepresentative>
<DefendantsInsurer/>
</DefendantRepresentative>
<MedicalReport MedicalReportStage2_1=""/>
<LossesToDate SeatbeltContributory="">
<ClaimantLosses/>
<DefendantReplies/>
<Total>
<LossesTotal CRUDeductions=""/>
</Total>
</LossesToDate>
<InterimPayment>  <ClaimantRequestForInterimPayment/>  <DefendantResponsesToInterimPaymentRequest/>
</InterimPayment>
<StatementOfTruth/>
<Phase2ClaimantDefendantDates>
<ClaimantRepresentativeDates/>
<DefendantRepresentativeDates/>
</Phase2ClaimantDefendantDates>
</InterimSettlementPack>
<Stage2SettlementPack>
<ClaimantRepresentative>
<CompanyDetails CompanyName="Morgan Cole" ContactName="a" ContactSurname="a" ReferenceNumber="a" TelephoneNumber="02008918273645">
<Address AddressType="A" City="City" Country="United Kingdom" HouseNumber="3" Street1="Street"/>
</CompanyDetails>
</ClaimantRepresentative>
<DefendantRepresentative>
<DefendantsInsurer/>
</DefendantRepresentative>
<MedicalReport MedicalReportStage2=""/>
<LossesToDate>
<ClaimantLosses/>
<DefendantReplies/>
<Total>
<CurrentTotal CRUDeductions=""/>
</Total>
</LossesToDate>
<AgreementData>
<FinalAgreementDetails>
<AgreementDetails/>
</FinalAgreementDetails>
</AgreementData>
<LastExtendedTime/>
<StatementOfTruth/>
<Phase2ClaimantDefendantDates>
<ClaimantRepresentativeDates/>
<DefendantRepresentativeDates/>
</Phase2ClaimantDefendantDates>
</Stage2SettlementPack>
<CourtProceedingsPack>
<ClaimantRepresentative>
<CompanyDetails CompanyName="Morgan Cole" ContactName="a" ContactSurname="a" ReferenceNumber="a" TelephoneNumber="02008918273645">
<Address AddressType="A"/>
</CompanyDetails>
</ClaimantRepresentative>
<DefendantRepresentative>
<DefendantsInsurer/>
</DefendantRepresentative>
<LossesToDate>
<ClaimantLosses/>
<DefendantReplies/>
</LossesToDate>
<CourtProceedingsPackPartA/>
<DisbursmentDisputed/>
<CourtProceedingsPackPartB>    <FixedCosts/>  </CourtProceedingsPackPartB>
<StatementOfTruth/>
<Phase2ClaimantDefendantDates>
<ClaimantRepresentativeDates/>
<DefendantRepresentativeDates/>
</Phase2ClaimantDefendantDates>
</CourtProceedingsPack>
<Timeouts>
<CurrentTimeout TimeOutType="" SelectedTimeOut="2010-05-07"/>
<PreCalculatedTimeouts>
<ClaimNotificationForm>
<Insurer LiabilityDecisionTimeout="" Stage1PaymentTimeout=""/>
<MIBorArt75 LiabilityDecisionTimeout="" Stage1PaymentTimeout=""/>
</ClaimNotificationForm>
<InterimSettlementPack>
<LowPayment InterimPaymentTimeout=""/>
<HighPayment InterimPaymentTimeout=""/>
</InterimSettlementPack>
<Stage2SettlementPack>
<Stage2DecisionOrCounterOffer Stage2DecisionOrCounterOfferTimeout=""/>
</Stage2SettlementPack>
<CourtProceedingsPack>
<CourtProceedings CourtProceedingsTimeout=""/>
</CourtProceedingsPack>
</PreCalculatedTimeouts>
</Timeouts>
</Data>

This is the relevant part of my script:

open(NEWFILE, '>C:\Perl\getHospitalsList\SendWaiting_Directory\GetClaim\Received\NewGetClaimData_Received.xml') or die $!;
my $secondfile= '\\\ITL019\SendWaiting_Directory\GetClaim\Received\TestGetClaimData_Received.xml';
my $secondparser = new XML::DOM::Parser;
my $seconddoc = $secondparser->parsefile($secondfile);
my $testvar=$seconddoc->getElementsByTagName('DefendantDetails')->item(0)->getFirstChild->getNodeValue;
print NEWFILE $testvar;
close NEWFILE;

I was hoping that this would print out the value of the DefendantDetails node and that, using this, I could navigate down to the PersonalDetails node and change the name to DefendantPersonalDetails. However, nothing is printed out! (there are no error messages though).

Any ideas anyone?
0
PERLfisher
Asked:
PERLfisher
1 Solution
 
wilcoxonCommented:
I would suggest using XML::Simple.  Then you can manipulate the XML structure as a plain perl nested hash.  XML::Simple does by default convert a lot of elements to attributes but it maintains the same nesting and meaning.  If you want things to remain as elements, you will need to change some of the options to XMLout and/or XMLin.
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;

my $xml = XMLin('tmp.in');
# to print out the DefendendtDetails
print Data::Dumper::Dumper($xml->{ClaimAndClaimantDetails}{DefendantDetails});
# to change the name of PersonalDetails
$xml->{ClaimAndClaimantDetails}{DefendantDetails}{DefendantPersonalDetails} = $x
ml->{ClaimAndClaimantDetails}{DefendantDetails}{PersonalDetails};
delete $xml->{ClaimAndClaimantDetails}{DefendantDetails}{PersonalDetails};
# output new file
open(NEWFILE, '>C:\Perl\getHospitalsList\SendWaiting_Directory\GetClaim\Received\NewGetClaimData_Received.xml') or die $!;
print NEWFILE XMLout($xml);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now