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?
PERLfisherAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.