Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to import a complex XML file into multiple SQL tables. I have tried using data maps but can only manage to import 1 node at a time. what I need to do is to be able to select foreign keys from parent nodes.

Can someone please help?

Here is a reduced version of my xml:
XML
<BookingList>
	<Booking>
	<BookingRef>HAY-171644</BookingRef>
	<BookingDate>10-15-12</BookingDate>	
		<ContactDetail>
		<Address1>My street name</Address1>		
		</ContactDetail>
		<Passengers>
		<PassengerDetail>
			<PassengerItem>1</PassengerItem>
			<Title>Miss</Title>
			<FirstName>Kerry</FirstName>
		</PassengerDetail>
		</Passengers>
		<BookingElements>
		<BookingElement>
			<BookingItem>1</BookingItem>
			<ElementRef>HAY-171644/1</ElementRef>
			<ElementType>Package</ElementType>
			<AccommodationDetail>
				<Accommodation>
					<AccommodationItem>1</AccommodationItem>
					<CheckIn>10-21-12</CheckIn>
					<CheckOut>10-28-12</CheckOut>
					<Rooms>
						<RoomItem>1</RoomItem>
						<RoomType> Double room</RoomType>
					</Rooms>
				</Accommodation>
			</AccommodationDetail>
		</BookingElement>
		</BookingElements>
	</Booking>
	<Booking>
	<BookingRef>HAY-171643</BookingRef>
	<BookingDate>10-15-12</BookingDate>
		<ContactDetail>
		<Address1>30 Street name</Address1>
		</ContactDetail>
		<Passengers>
		<PassengerDetail>
			<PassengerItem>1</PassengerItem>
			<Title>Mr</Title>
			<FirstName>ALWYN</FirstName>
		</PassengerDetail>
		</Passengers>
		<BookingElements>
		<BookingElement>
			<BookingItem>1</BookingItem>
			<ElementRef>HAY-171643/1</ElementRef>
			<ElementType>Package</ElementType>
			<AccommodationDetail>
				<Accommodation>
					<AccommodationItem>1</AccommodationItem>
					<CheckIn>06-19-13</CheckIn>
					<CheckOut>06-26-13</CheckOut>
						<Rooms>
							<RoomItem>1</RoomItem>
							<RoomType>Studio for 4</RoomType>
						</Rooms>
				</Accommodation>
			</AccommodationDetail>
		</BookingElement>
		</BookingElements>
	</Booking>
</BookingList>
Posted
Comments
Maciej Los 18-Oct-12 14:07pm    
... and the data structure of these tables is...
milenalukic 18-Oct-12 14:32pm    
A table for each node.
boking table with bookingRef and date.
contact table with bookingref,addreass1.
passenger table with bookingref,passenger item,title,firstname.
bookingelement table woth bookingref,bookingitem,elementref,elementtype.

And so on.

My problem is how to get the bookingref to read with all the other info as this will be the foreign key in most cases.

If you can show me an example then I will work out the rest.

Thanks in advance.

1 solution

I don't know the exact answer, but i hope it will help you to find a solution:
SQL
DECLARE @iDoc INT
DECLARE @xml as XML 

SET @xml = N'<bookinglist>...</bookinglist>'

--Booking
EXEC sp_xml_preparedocument @iDoc OUTPUT, @xml
SELECT *
FROM OPENXML(@iDoc,'/BookingList/Booking',2)
WITH ([BookingRef] NVARCHAR(50)  'BookingRef',
	[BookingDate] NVARCHAR(20)  'BookingDate')
EXEC sp_xml_removedocument @iDoc

--ContactDetail
EXEC sp_xml_preparedocument @iDoc OUTPUT, @xml
SELECT *
FROM OPENXML(@iDoc,'/BookingList/Booking/ContactDetail',2)
WITH ([Address] NVARCHAR(50)  'Address1')
EXEC sp_xml_removedocument @iDoc

--Passengers
EXEC sp_xml_preparedocument @iDoc OUTPUT, @xml
SELECT *
FROM OPENXML(@iDoc,'/BookingList/Booking/Passengers/PassengerDetail',2)
WITH ([PassengerItem] INT  'PassengerItem',
	[Title] NVARCHAR(20)  'Title',
	[FirstName] NVARCHAR(20)  'FirstName')
EXEC sp_xml_removedocument @iDoc
 
Share this answer
 

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