Page 1 of 1

XML parsing - duplicate tag name

Posted: Mon Aug 09, 2004 8:09 am
by leenoble_uk
Just started my first XML parsing project and I've hit a snag.
The XML doc I have been supplied uses the same tag name in different parts of the tree.
So my basic code replaces the former entry with the latter one.
Here's a single entry in the document:

Code: Select all

<Product Code="GBWTB0008R" DefaultLanguage="" WTBReference="">
	  <Name>My golf club</Name>
	  <ProductURL></ProductURL>
	  <ProductDetailsURL>http://somewhere</ProductDetailsURL>
	  <Flags DisplayOnly="Y" PublishingFlag="Y"/>
	  <Status ProductStatus="Ok" DAStatus="Approved"/>
	  <Category Code="R" Name="RECREATION/LEISURE/SPORTS ACTIVITIES"/>
	  <SubCategory Code="4GO" Name="GOLF"/>
	  <SubCategory Code="4GC" Name="GOLF-OTHER COURSES"/>
	  <BookingCompanyDetails OperatorOrRepresentative="O">
	    <Name>My Golf Club</Name>
	    <Address>
	      <Line1>Long Road</Line1>
	      <Line2></Line2>
	      <Town>Golftown</Town>
	      <PostTown>Golftown</PostTown>
	      <County>Teeside</County>
	      <State>ENGLAND</State>
	      <PostCode>TS3 7TR</PostCode>
	      <Country>UNITED KINGDOM</Country>
	    </Address>
	    <Title>Mr</Title>
	    <FirstName>Stuart</FirstName>
	    <LastName>Wilson</LastName>
	    <Phone>656565</Phone>
	    <Fax>787878</Fax>
	    <Email></Email>
	  </BookingCompanyDetails>
	  <ProductAddress>
	    <Line1>Long Road</Line1>
	    <Line2></Line2>
	    <Town>Green Bunker</Town>
	    <PostTown>Green Bunker</PostTown>
	    <County>Apron</County>
	    <PostCode>AP6 5RT</PostCode>
	  </ProductAddress>
	  <MapCoordinates North="190874" East="259489"/>
	  <OperatingInformation Keyword="YEAR ESTABLISHED" Type="Text">
	    <Data>1993</Data>
	    <Remarks>Founded 1993</Remarks>
	  </OperatingInformation>
	  <OperatingInformation Keyword="PREREQUISITE" Type="Single Choice">
	    <Data>APPLY</Data>
	    <Remarks>Handicap Certificate required</Remarks>
	  </OperatingInformation>
	  <OperatingInformation Keyword="OPERATING PROFILE" Type="Operating Profile">
	    <Data>-1</Data>
	    <Remarks>.</Remarks>
	  </OperatingInformation>
	  <Features Group="Services" Keyword="MEALS">
	    <Remarks>Meals available</Remarks>
	  </Features>
	  <Features Group="Highlights" Keyword="HIGHLIGHT 1">
	    <Remarks>A 4580 yard-long, 18 hole course with par 66 and SSS of 63</Remarks>
	  </Features>
	  <Features Group="Highlights" Keyword="HIGHLIGHT 2">
	    <Remarks>Pay & play (no handicap)</Remarks>
	  </Features>
	  <Features Group="Highlights" Keyword="HIGHLIGHT 3">
	    <Remarks>Putting green and practise area</Remarks>
	  </Features>
	  <Features Group="Highlights" Keyword="HIGHLIGHT 4">
	    <Remarks>Golf lessons available</Remarks>
	  </Features>
	  <Features Group="Facilities" Keyword="ACCOMMODATION">
	    <Remarks>Accommodation arranged locally</Remarks>
	  </Features>
	  <Features Group="Services" Keyword="EQUIPMENT HIRE">
	    <Remarks>Club and trolley hire</Remarks>
	  </Features>
	  <GeneralInformation>9 hole course. Parkland. Secretary: Stuart Wilson.</GeneralInformation>
	  <Grading GradingScheme="Not Applicable" GradingDesignator="" GradingStatus="NOT APPROPRIATE" GradingStar=""/>
	  <OperatingCycles/>
	</Product>
So the first sticking point I've come to is <PostCode>.
There are two entries for postcode, one for location and one for booking address.
When parsed the second postcode overwrites the first because they have the same tag name.
Is there a way to refer to each individually using parent tag names like
BookingCompanyDetails/Postcode
and
Product Address/Postcode

How do I do this in the PHP code? I did a search and I saw someone had used ^ between tag names so I tried this but it didn't work.

Alternatively should I just add another level to my catching array so I have $arrayName["ResultsCounter"]["postcode"][0] and $arrayName["ResultsCounter"]["postcode"][1] and then pick them apart afterwards?
What happens in this case if the first postcode doesn't exist? Would I end up with the second postcode in the place where the first should be for that entry and no second entry?
What's the usual way of dealing with this kind of problem?
Cheers
Lee