Re: Import XML from Command-Line into SQL

From: Graeme Malcolm (graemem_cm_at_hotmail.com)
Date: 08/23/04


Date: Mon, 23 Aug 2004 08:59:50 +0100

I'd probably create a mapping schema and a VB Script that uses the SQL XML
Bulk Load component to bulk load the data. Download SQLXML
(http://www.microsoft.com/downloads/details.aspx?familyid=4c8033a9-cf10-4e22-8004-477098a407ac&displaylang=en)
and have a look at the documentation. Here's a simple example to get you
started (It loads data into the Categories and Products tables in the
Northwind database):

XML file (Catalog.xml):
<?xml version="1.0" ?>
<Catalog>
  <Category CategoryID="99">
    <CategoryName>Scottish Foods</CategoryName>
    <Description>Traditional food from Scotland</Description>
    <Product ProductID="101">
      <ProductName>Porridge</ProductName>
      <UnitPrice>16</UnitPrice>
    </Product>
    <Product ProductID="102">
      <ProductName>Haggis</ProductName>
      <UnitPrice>19</UnitPrice>
    </Product>
  </Category>
  <Category CategoryID="100">
    <CategoryName>Scottish Drinks</CategoryName>
    <Description>Traditional drinks from Scotland</Description>
    <Product ProductID="103">
      <ProductName>Single Malt Whisky</ProductName>
      <UnitPrice>100</UnitPrice>
    </Product>
  </Category>
</Catalog>

Schema file (Catalog.xsd):
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="Catalog" sql:is-constant="1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Category" sql:relation="Categories">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="CategoryName" type="xsd:string"
                           sql:field="CategoryName"
sql:datatype="nvarchar(15)" />
              <xsd:element name="Description" type="xsd:string"
                           sql:field="Description" sql:datatype="ntext" />
              <xsd:element name="Product" sql:relation="Products">
                <xsd:annotation>
                  <xsd:appinfo>
                    <sql:relationship parent="Categories"
                                      parent-key="CategoryID"
                                      child="Products"
                                      child-key="CategoryID" />
                  </xsd:appinfo>
                </xsd:annotation>
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="ProductName" type="xsd:string"
                                 sql:field="ProductName"
sql:datatype="nvarchar(40)" />
                    <xsd:element name="UnitPrice" type="xsd:decimal"
                                 sql:field="UnitPrice" sql:datatype="money"
/>
                  </xsd:sequence>
                  <xsd:attribute name="ProductID" type="xsd:integer"
                                 sql:field="ProductID" sql:datatype="int" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="CategoryID" type="xsd:integer"
                           sql:field="CategoryID" sql:datatype="int" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

VB Script:
Set objBulkLoad = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad")
objBulkLoad.ConnectionString = _
    "provider=SQLOLEDB;Server=(local);database=Northwind;" & _
    "Integrated Security=SSPI;"
objBulkLoad.Execute "Catalog.xsd", "Catalog.xml"
Set objBulkLoad = Nothing
MsgBox "Catalog Imported"

Hope that helps,
Graeme

-- 
----
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
"Sean" <sean_cottrell@hotmail.com> wrote in message 
news:aadd01c4888c$71c28930$a501280a@phx.gbl...
HI,
I am try to accomplish the following:
Every day I will get a random number of XML files with a
random number of row entries.  each of these files will
have a different file name but be in a specific file name
format such as file1.xml,file2.xml, etc. These files will
be "dumped" to a network drive at random times.  We
already have a program written that will monitor this
directory and when I file gets "dumped" into it, will
fire of a script.
I am looking for the best way to take each of these .xml
files and import them into a MS SQL Table. The files are
very simple containing only 9 columns per row of data.
Also, we would prefer that this script execute from a
remote server other than the server that is running MS
SQL.
Can anybody suggest the best way to do this? 


Relevant Pages

  • Re: Incremental population stops
    ... I've attached a sql script file ... MSSearch service is not able to take the push of data from SQL Server at ... While the XML Ifilters might not be the solution for this issue, ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Import XML from Command-Line into SQL
    ... >>I'd probably create a mapping schema and a VB Script ... >that uses the SQL XML ... >>Bulk Load component to bulk load the data. ... >>remote server other than the server that is running MS ...
    (microsoft.public.sqlserver.xml)
  • Re: XML IMPORT
    ... this site to import XML into SQL using some VB Script in a DTS package, sorry about the long description there, but for some reason I am unable to make it work, I am trying to get a selection of information out of one tag but the DTS says it has completed but nothing is there, I think it has something to do with the way that the XML file is stuctured:- the xml files looks like ... If so I'm wondering if the script is having problems with the second document prologue. ... is SQL Server 2005 and SIS an option for you? ...
    (microsoft.public.sqlserver.xml)
  • Re: Need suggestion: importing data into SQL Express
    ... be exposed by a DataReader into a table on the SQL ... SQL Bulk Load is another option to get it into the table. ... Do you recommend importing the data as XML into like a dataset, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Is there a way to convert ms sql 2008 tables to XML files?
    ... After looking at the code I made it work, since I removed 2 nVARCHARcolumns from the SQL statement where it is used as an RTF. ... I have the 2 XML files the bad and the good which are below: ... Verdana;}{\f1\fnil\fcharset0 Microsoft Sans ... Here is the actual string copied from the ms sql 2008 which works fine; ...
    (microsoft.public.dotnet.framework.aspnet)