/ Published in: SQL
Parse xml (passed as a parameter) in SQL
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- FIRST WAY DECLARE @xml AS xml; SET @xml = '<LandLord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Address1>1092 Bel Lido Dr</Address1> <City>Highland Beach</City> <County>Palm Beach County</County> <State>FL</State> <Zip>33487</Zip> <DispStreetView>true</DispStreetView> <UserId>271111</UserId> <PropertyId>1234</PropertyId> </LandLord>' SELECT Tbl.Col.value('Address1[1]', 'varchar(100)') AS address1, Tbl.Col.value('City[1]', 'varchar(100)') AS City, Tbl.Col.value('County[1]', 'varchar(100)') AS County, Tbl.Col.value('State[1]', 'varchar(100)') AS State, Tbl.Col.value('DispStreetView[1]', 'bit') AS DisplayStreetView, Tbl.Col.value('UserId[1]', 'int') AS UserId FROM @xml.nodes('//LandLord') Tbl(Col) -- SECOND WAY DECLARE @idoc INT; DECLARE @doc VARCHAR(1000); SET @doc =' <ROOT> <Customer CustomerID="VINET" ContactName="Paul Henriot"> <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00"> <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/> <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/> </Order> </Customer> <Customer CustomerID="LILAS" ContactName="Carlos Gonzlez"> <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00"> <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/> </Order> </Customer> </ROOT>' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Execute a SELECT statement that uses the OPENXML rowset provider. SELECT * FROM OPENXML (@idoc, '/ROOT/Customer',1) WITH (CustomerID VARCHAR(10), ContactName VARCHAR(20)) EXEC sp_xml_removedocument @idoc