February 23, 2011 at 8:38 am
Hi,
i need to read the file in the internet to my SQL Server directly, like this:
-----------------------------------------
DECLARE @CD TABLE (XMLData XML);
INSERT INTO @CD
SELECT *
FROM OPENROWSET(BULK N'http://www.w3schools.com/XML/cd_catalog.xml', SINGLE_BLOB) rs;
INSERT INTO dbo.CD_Info (Title, Artist, Country, Company, Price, YearReleased)
SELECT Title = x.data.value('TITLE[1]','varchar(100)'),
Artist = x.data.value('ARTIST[1]','varchar(100)'),
Country = x.data.value('COUNTRY[1]','varchar(25)'),
Company = x.data.value('COMPANY[1]','varchar(100)'),
Price = x.data.value('PRICE[1]','numeric(5,2)'),
YearReleased = x.data.value('YEAR[1]','smallint')
FROM @CD t
CROSS APPLY t.XMLData.nodes('/CATALOG/CD') x(data);
----------------------------------
it is possible?
The article is perfect for me but I can not use the c:\doc.xml, I have to use the http://xxxxxxx.xxxx.xml
I have an ASP page that reads the XML directly from the Internet, but I do not want my clients to read directly from the Internet, I want to read from my SQL server.
---------------
the page is like this:
<%@ LANGUAGE="VBScript" CodePage ="1252" %>
<!--#include file="CnnInc.asp"-->
<html>
<head>
<title>teste</title>
</head>
<body>
<h1>Noticias from XML</h1>
<script language="JavaScript">
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function showhide(element){
var e=document.getElementById(element);
if (e.style.display == "block")
e.style.display = "none";
else
e.style.display = "block";
}
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("http://www.jornaldenegocios.pt/funcionalidades/envio_terceros/index.php?.....user/password.....")
//xmlDoc.load("noticias2.xml")
nodes = xmlDoc.documentElement.childNodes
document.write(nodes.length + " noticias");
for (i=0;i<nodes.length;i++)
if (nodes.item(i).nodeType==1) {
nodes2 = nodes(i).childNodes
//if (trim(nodes2.item(1).text) == "Mercados" || trim(nodes2.item(1).text) == "Economia") {
document.write("<div class='noticia'><a href='#' onclick='showhide(\"noticia" + i + "\")'><strong> " + nodes2.item(1).text + "</strong> " + nodes2.item(2).text + "</a><br />")
document.write("<div id='noticia" + i + "' class='artigo' style='display: none;'>" + nodes2.item(3).text + "</div>")
document.write("</div>")
//}
}
</script>
</body>
</html>
---------------------------------------
February 23, 2011 at 12:04 pm
jorge_gomes98,
please open a separate thread for the issue you're struggling with.
The section you posted in should be realated to the article. More related than "it's an xml issue"....
October 17, 2011 at 5:51 am
To add a valuable resource, I suggest you watch the 10 videos (they are free for viewing online) on the link below:
October 18, 2011 at 4:10 am
MrBool.US (10/17/2011)
To add a valuable resource, I suggest you watch the 10 videos (they are free for viewing online) on the link below:
Interesting set of videos.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
May 31, 2013 at 6:52 am
While it offers a very brief sample to get going fast it should at least list other alternatives since some of them are quite nice. Readers need to be aware of as many alternatives as possible since some technologies have size and performance limitations.
I load massive XML files with SQLXMLBulkload:
Just supply a connection string, a XSD and the XML file and you can load directly into any table.
and it even supports an error log should anything go wrong and you can control it from a client whether it be C# or just a simple VBS like below:
Dim objBL
set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")
objBL.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=dbo_Dfrndis;Password=abc1234;Initial Catalog=frndis;Data Source=sqldev"
objBL.ErrorLogFile = "C:\MSSQL\clients\INTLdist\CRD\XLT\error.xml"
objBL.Execute "CRD.xsd", "CRD20130429122127010074ToCRD-2013-05-01_02-59-43-PM.xml"
set objBL=Nothing
Sid
May 31, 2013 at 9:09 am
even though u successfully loaded the raw xml into a table you still processed the data one row at a time. It is much better to use OPENXML and pivot the data.
example:
--TABLE TO HOLD THE RAW XML
DECLARE @tbl TABLE
(
ID INT, ParentID INT, NodeType INT, LocalName NVARCHAR(100), Prefix VARCHAR(50),
NameSpaceURI VARCHAR(50), DataType VARCHAR(50), Prev VARCHAR(50), [TEXT] NVARCHAR(200)
)
--xml in a variable of XML type that might be used in a proc or fnc
EXEC sp_xml_preparedocument @docHandle OUTPUT,@ItemsXML;
INSERT INTO @tbl(
ID ,ParentID ,NodeType ,LocalName,Prefix ,NameSpaceURI ,DataType ,Prev ,[TEXT]
)
SELECT * FROM OPENXML(@docHandle, '/PackageRequests/ItemRequest',1)
EXEC sp_xml_removedocument @docHandle
;
WITH CTE_Ranked AS (
SELECT
T1.LocalName
,T2.Text
,NTILE(12) OVER (PARTITION BY T1.LocalName ORDER BY T1.ID) AS RNKING
--NTILE Count defined by counting the largest number of possible records for any one column
--this could easily be a variable
FROM @tbl T1
INNER JOIN @Tbl T2
ON T1.ID = T2.ParentID
WHERE T1.LocalName IN ('VndID','LocID')
),
CTE_PIVOT_SOURCE AS (
SELECT
CTE_Ranked0.LocalName AS RLN
,CTE_Ranked0.text AS RIV
,CTE_Ranked01.RNKING
FROM CTE_Ranked CTE_Ranked0
INNER JOIN CTE_Ranked CTE_Ranked01 ON CTE_Ranked01.RNKING = CTE_Ranked0.RNKING
)
SELECT VendorID,LocationID
FROM CTE_PIVOT_SOURCE
PIVOT (
MIN(RIV)
FOR RLN IN (VndID,LocID)
) AS PivotTable
May 31, 2013 at 10:44 am
Responded to wrong post...
May 31, 2013 at 10:57 am
This took all of five minutes to copy the code, test it out, and save it for future use.
The article was clear, simple, and short. The need to parse XML data is not uncommon for me, but it is infrequent. So instead of reinventing a solution, I may be able to save a few hours here and there by starting with your solution.
Thanks!
Jim Peters
May 31, 2013 at 4:54 pm
Nice Article! I first started working with XML and .Net web services about 10 years ago as a developer.
These days I wear a DBA hat and just had to write a load process for several XML interfaces. Wish I had seen this first! I found SSIS to be fairly limited and clunky compared to my earlier .Net experiences.
I settled on using the XML Source. I ended up writing a schema validation and XSLT piece to make the XML palatable for the XML Source object. XML Source wasn't very flexible, and certainly wasn't as fast as a bulk load.
June 1, 2013 at 6:49 am
dg81328 (5/31/2013)
even though u successfully loaded the raw xml into a table you still processed the data one row at a time. It is much better to use OPENXML and pivot the data.
I'm not sure how you can say that so please explain. From what I can see, the methods in the article will allow you to handle multiple XML documents that are loaded into a table (@CD in the article is a table variable) without RBAR whereas the OPENXML document that you suggest can only handle one XML document at a time.
Further, and I admit that I haven't tested it, I suspect that code you posted will be slower because of the PIVOT.
Like I said, please explain your claims about why you think the use of OPENXML and a pivot provides any advantage of the method in the article because I'm just not seeing it and am always interested in better ways.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 1, 2013 at 2:27 pm
Great article. For those too young to know, Empire Burlesque was a towering collaboration between Mark Knopfler and Bob Dylan that never really got the credit it deserved. That it was wrested back from obscurity into an odd new kind of limelight made the article all the more enjoyable!
...One of the symptoms of an approaching nervous breakdown is the belief that ones work is terribly important.... Bertrand Russell
June 2, 2013 at 12:59 pm
Spam reported.
...One of the symptoms of an approaching nervous breakdown is the belief that ones work is terribly important.... Bertrand Russell
June 2, 2013 at 10:04 pm
Jim, boumerlin - Thanks! I'm glad that you were able to add this solution to your toolbox.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 3, 2013 at 10:54 pm
jcasement (5/31/2013)
While it offers a very brief sample to get going fast it should at least list other alternatives since some of them are quite nice. Readers need to be aware of as many alternatives as possible since some technologies have size and performance limitations.I load massive XML files with SQLXMLBulkload:
Just supply a connection string, a XSD and the XML file and you can load directly into any table.
and it even supports an error log should anything go wrong and you can control it from a client whether it be C# or just a simple VBS like below:
Dim objBL
set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")
objBL.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=dbo_Dfrndis;Password=abc1234;Initial Catalog=frndis;Data Source=sqldev"
objBL.ErrorLogFile = "C:\MSSQL\clients\INTLdist\CRD\XLT\error.xml"
objBL.Execute "CRD.xsd", "CRD20130429122127010074ToCRD-2013-05-01_02-59-43-PM.xml"
set objBL=Nothing
Sid
What's the largest file you've loaded with it? And I hope the connection string you just posted doesn't actually contain valid login info.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 19, 2014 at 2:25 am
Thank you for sharing this wonderful post.
Viewing 15 posts - 46 through 60 (of 60 total)
You must be logged in to reply to this topic. Login to reply