sharon
sharon

Reputation: 11

How to load an xml file into a SQL Server table

I searched in the internet and there is not good example for converting xml file into SQL Server table.

I have this file:

<http://www.ims.gov.il/ims/PublicXML/isr_cities.xml>

This is a weather file for 15 cities (for the next 4 days).

How can I load it to a flat table in SQL Server?

I try and it just give me null's :-(

I put out some value's that made problems but still I can't convert it to a big flat table.

How can I put out all the nodes inside the xml file?

Thanks!

Upvotes: 0

Views: 1029

Answers (4)

sharon
sharon

Reputation: 11

the problem was in my local setting...

here anyway my code after improving. I still working on it... thank's to all of you!

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)

Upvotes: 1

sharon
sharon

Reputation: 11

I must need the element node, but for begining I did something like this:

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]

after that i get in the results:

<LocationId>520</LocationId> 

and so on... instead of just 520...

but again, if i get the element node i will be happy...

Upvotes: 0

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

I think this might be what you want. After the call you can do with the XML what ever you want...

--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;

Upvotes: 1

marc_s
marc_s

Reputation: 754488

You can try something like this (assuming you have your XML in a SQL Server variable called @input 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)

That should give you at least some output and can server as a starting point for more exploration!

Upvotes: 1

Related Questions