2011-02-15 128 views
0

我需要将一些XML数据导入到SQL Server Express 2005中,我想知道如何做到这一点?下面是我需要导入的xml示例。如果必须的话,我可以写一些代码来完成它。但我更喜欢更简单的方法。我正在使用SQL Server Express 2005,所以看起来就像使用SSIS一样。将xml数据导入到SQL Server Express 2005中的最佳方法

<?xml version="1.0" encoding="UTF-8" ?> 
<tour> 
    <tourName>Highlights of Jordan</tourName> 
    <dossierCode>DWHJ</dossierCode> 
    <tripDescription>Explore ancient sites and lost cities, overnight in a Bedouin tent and dine with the locals amidst the dramatic desert scenery of Wadi Rum. Take in Petra's fascinating history and marvel at its thrilling beauty—and still have a chance to relax in the therapeutic waters of the Dead Sea. This eight-day trip is ideal for the traveller who is short on time but wants to soak up the colours, culture and history of a country that's small in size but big in adventure.</tripDescription> 
    <tripStyle>Classic</tripStyle> 
    <year>2011</year> 
    <brochureSupplier>Gap Adventures</brochureSupplier> 
    <tripCountries>Jordan</tripCountries> 
    <tripContinents>North Africa/Middle East</tripContinents> 
    <startCountry>Jordan</startCountry> 
    <finishCountry>Jordan</finishCountry> 
    <startCity>Amman</startCity> 
    <finishCity>Amman</finishCity> 
    <tripActivities>Culture/History</tripActivities> 
    <duration>8</duration> 
    <physicalGrading>3</physicalGrading> 
    <serviceLevel>Standard</serviceLevel> 
    <groupSize> 
    <minimum>5</minimum> 
    <maximum>15</maximum> 
    </groupSize> 
    <briefItinerary> 
    <briefItineraryItem label="Day 1 Amman">Arrive at any time.</briefItineraryItem> 
    <briefItineraryItem label="Day 2 Jerash, the Dead Sea (B)">Guided visit to the ancient Roman town of Jerash, trip to Dead Sea.</briefItineraryItem> 
    <briefItineraryItem label="Days 3-4 Petra (2B)">Madaba mosaics, Mount Nebo, Kerak Castle, Petra entrance and guided tour.</briefItineraryItem> 
    <briefItineraryItem label="Day 5 Wadi Rum (B,D)">Wadi Rum 4x4 jeep excursion, dinner and night in desert camp.</briefItineraryItem> 
    <briefItineraryItem label="Day 6 Aqaba (B)">Optional snorkelling excursion.</briefItineraryItem> 
    <briefItineraryItem label="Day 7 Amman (B)">Time to explore Amman.</briefItineraryItem> 
    <briefItineraryItem label="Day 8 Amman (B)">Depart at any time.</briefItineraryItem> 
    </briefItinerary> 
    <detailedItinerary> 
    <detailedItineraryItem label="Day 1 Arrive Amman">You are welcome to arrive in Amman at any time, as today is a designated arrival day with no planned activities. Please note that there will be an important welcome meeting this evening at our joining hotel. Please check the hotel notice board for more information on the location of this meeting or ask at reception. Please bring your passport and travel insurance documents to this meeting, as you will be required to fill out some important pre-trip admin documents. After learning more about our tour and plans for the following day, you are welcome to join our leader and your travelling companions for an optional dinner nearby. Overnight in Amman.</detailedItineraryItem> 
    <detailedItineraryItem label="Day 2 Jerash, Dead Sea (B)">After breakfast at the hotel we will set off on our half day trip to Jerash, a fascinating place and one of the largest and most well preserved Roman sites outside Italy. Its paved and colonnaded streets, soaring hilltop temples, handsome theatres, spacious public squares and plazas, baths, fountains and city walls pierced by towers and gates remain in exceptional condition. The drive takes less than an hour each way, but will transport you some 2000 years back in time. After lunch, we head to the Dead Sea. The Dead Sea is actually a lake, without any from of life whatsoever existing in its waters. The water is so dense that it is practically impossible to sink, being five to ten times as salty as regular sea water. We base ourselves at a resort for the day, where you will have access to the beach. While swimming, cover yourself with the sea's mud, which is reputed to have special healing powers, and don't forget to take a photo of yourself reading a book while floating! You will notice that the salt penetrates any cuts on your body and has an unusual smell, but there are showers to wash off under after swimming. Afterwards, while away under an umbrella next to one of the fresh water swimming pools. There are a few options for your lunch today and the choice is yours, ranging from a full buffet at the resort or a snack from the cafe. We will return to Amman for the evening. Overnight in Amman. Approx travel times: Amman to Jerash, 2hrs (return); Amman to Dead Sea, 4hrs (return)</detailedItineraryItem> 
    </detailedItinerary> 
</tour> 

回答

0

OPENROWSET支持批量关键字,这有助于我们在SQL Server 2005在这里导入XML文件是我发现了一个例子:

CREATE TABLE XmlImportTest 
(
xmlFileName VARCHAR(300), 
xml_data xml 
) 
GO 

DECLARE @xmlFileName VARCHAR(300) 
SELECT @xmlFileName = 'c:\TestXml.xml' 
-- dynamic sql is just so we can use @xmlFileName variable in OPENROWSET 
EXEC(' 
INSERT INTO XmlImportTest(xmlFileName, xml_data) 

SELECT ''' + @xmlFileName + ''', xmlData 
FROM 
(
SELECT * 
FROM OPENROWSET (BULK ''' + @xmlFileName + ''' , SINGLE_BLOB) AS XMLDATA 
) AS FileImport (XMLDATA) 
') 
GO 
SELECT * FROM XmlImportTest 

DROP TABLE XmlImportTest 

Reference

+0

是否有办法打破数据并将其转储到特定的字段? – SuperFurryToad 2011-02-15 13:48:43

0

你可以在这里阅读http://msdn.microsoft.com/en-us/library/ms345117%28v=sql.90%29.aspx关于XML支持SQL Server 2005.

一些代码显示你可以做什么。

declare @xml as xml 

set @xml = 
'<?xml version="1.0" encoding="UTF-8" ?> 
<tour> 
    <tourName>Highlights of Jordan</tourName> 
    <dossierCode>DWHJ</dossierCode> 
    <tripStyle>Classic</tripStyle> 
    <year>2011</year> 
</tour> 
' 

select 
    t.value('tourName[1]', 'varchar(50)'), 
    t.value('dossierCode[1]', 'varchar(50)'), 
    t.value('tripStyle[1]', 'varchar(50)'), 
    t.value('year[1]', 'int') 
from @xml.nodes('tour') t(t) 
0

感谢您的回复。最后,我只写了一些代码来解析XML,并进行了批量插入

相关问题