我在互联网上搜索过,并没有将xml文件转换为SQL Server表的好例子。
我有这个文件:
<http://www.ims.gov.il/ims/PublicXML/isr_cities.xml>
这是15个城市的天气文件(接下来的4天)。
如何将其加载到SQL Server中的平面表?
我尝试它只是给我null: - (
我提出了一些产生问题的价值,但我仍然无法将其转换为大型平台。
如何将xml文件中的所有节点都放出来?
谢谢!
答案 0 :(得分:1)
您可以尝试这样的方法(假设您在名为@input XML
的SQL Server变量中包含XML):
SELECT
LocationId = XC2.value('(LocationId)[1]', 'int'),
LocNameEng = XC2.value('(LocationNameEng)[1]', 'varchar(100)'),
TheDate = XCE.value('(../Date)[1]', 'DATE'),
ElementName = XCE.value('(ElementName)[1]', 'varchar(50)'),
ElementValue = XCE.value('(ElementValue)[1]', 'varchar(50)')
FROM
@input.nodes('/IsraelCitiesWeatherForecastEvening/Location') AS XT(XC)
CROSS APPLY
XC.nodes('LocationMetaData') AS XT2(XC2)
CROSS APPLY
XC.nodes('LocationData/TimeUnitData/Element') AS XTE(XCE)
这应该至少给你一些输出,并且可以作为更多探索的起点服务器!
答案 1 :(得分:1)
我认为这可能就是你想要的。在通话结束后,您可以根据需要使用XML ...
--you need to allow this:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
DECLARE @URL VARCHAR(MAX) = 'http://www.ims.gov.il/ims/PublicXML/isr_cities.xml';
DECLARE @xmlT TABLE (yourXML XML);
DECLARE @Response NVARCHAR(MAX);
DECLARE @XML XML;
DECLARE @Obj INT;
DECLARE @Result INT;
DECLARE @HTTPStatus INT;
DECLARE @ErrorMsg NVARCHAR(MAX);
EXEC @Result = sp_OACreate 'MSXML2.XMLHttp', @Obj OUT ;
EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'GET', @URL, false;
EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded';
EXEC @Result = sp_OAMethod @Obj, SEND, NULL, '';
EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT ;
INSERT @xmlT ( yourXML )
EXEC @Result = sp_OAGetProperty @Obj, 'responseXML.xml';
SELECT * FROM @xmlT;
答案 2 :(得分:1)
问题出在我当地的环境......
无论如何我的代码经过改进。我还在努力...... 谢谢你们所有人!
DECLARE @WeatherXML xml
SET @WeatherXML = ( SELECT * FROM
OPENROWSET (BULK 'C:\Stam\isr_cities.xml', SINGLE_BLOB) AS FileImport (XMLDATA))
SELECT
wXML.query('../../../LocationMetaData/LocationId').value('.','NVARCHAR(30)'),
wXML.query('../../../LocationMetaData/LocationNameEng').value('.','NVARCHAR(30)'),
wXML.query('../../../LocationMetaData/LocationNameHeb').value('.','NVARCHAR(30)'),
wXML.query('../../../LocationMetaData/DisplayLat').value('.','NVARCHAR(30)'),
wXML.query('../../../LocationMetaData/DisplayLon').value('.','NVARCHAR(30)'),
wXML.query('../Date').value('.','NVARCHAR(30)'),
wXML.query('ElementName').value('.','NVARCHAR(30)'),
wXML.query('ElementValue').value('.','NVARCHAR(30)')
FROM @WeatherXML.nodes('/IsraelCitiesWeatherForecastMorning/Location/LocationData/TimeUnitData/Element') AS XmlData(wXML)
答案 3 :(得分:0)
我必须需要元素节点,但是为了开始,我做了类似的事情:
DECLARE @WeatherXML xml
SET @WeatherXML = ( SELECT * FROM
OPENROWSET (BULK 'C:\Stam\isr_citiesNEW.xml', SINGLE_BLOB) AS FileImport (XMLDATA))
-- insert xml to [MOT_Temp_WeatherXML] table
INSERT INTO [dbo].[MOT_Temp_WeatherXML]
([LocationId]
,[LocationNameEng]
,[LocationNameHeb]
,[DisplayLat]
,[DisplayLon]
)SELECT
wXML.query('LocationId').value('.','NVARCHAR(30)'),
wXML.query('LocationNameEng').value('.','NVARCHAR(30)'),
wXML.query('LocationNameHeb').value('.','NVARCHAR(30)'),
wXML.query('DisplayLat').value('.','NVARCHAR(30)'),
wXML.query('DisplayLon').value('.','NVARCHAR(30)')
FROM @WeatherXML.nodes('/IsraelCitiesWeatherForecastMorning/Location/LocationMetaData') AS XmlData(wXML)
SELECT * FROM [MOT_Temp_WeatherXML]
之后我进入结果:
<LocationId>520</LocationId>
等等......而不只是520 ......
但是,如果我得到元素节点,我会很高兴...