简体   繁体   中英

How can i load an XML file to SQL

Im trying to load an XML file to SQL Server, but im not getting anything Here is my XML File:

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31" catalog-id="master-catalog-sso-us">
    <header>
        <image-settings>
            <internal-location base-path="/"/>
            <view-types>
                <view-type>large</view-type>
                <view-type>medium</view-type>
                <view-type>small</view-type>
                <view-type>swatch</view-type>
            </view-types>
            <alt-pattern>${productname}</alt-pattern>
            <title-pattern>${productname}</title-pattern>
        </image-settings>
    </header>

    <category category-id="root">
        <display-name xml:lang="x-default">root Category</display-name>
        <description xml:lang="x-default">root Category</description>
        <online-flag>true</online-flag>
        <template/>
        <page-attributes/>
        <refinement-definitions>
            <refinement-definition type="attribute" bucket-type="none" attribute-id="shops" system="false">
                <display-name xml:lang="x-default">Shops</display-name>
                <value-set>search-result</value-set>
                <sort-mode>value-name</sort-mode>
                <sort-direction>ascending</sort-direction>
                <cutoff-threshold>5</cutoff-threshold>
            </refinement-definition>
        </refinement-definitions>
    </category>

    <category category-id="default">
        <display-name xml:lang="x-default">default Category</display-name>
        <description xml:lang="x-default">default Category</description>
        <online-flag>true</online-flag>
        <parent>root</parent>
        <template/>
        <page-attributes/>
    </category>

    <product product-id="0217328320-sso-us">
        <ean/>
        <upc/>
        <unit/>
        <min-order-quantity>1</min-order-quantity>
        <step-quantity>1</step-quantity>
        <display-name xml:lang="x-default">Magnetibook - 4 Seasons</display-name>
        <long-description xml:lang="x-default">48 magnets that stick to the metallic &amp;#34;canvas&amp;#34; where your child can choose between any of the four season &amp;#34;back drops&amp;#34; and then pick out the appropriate outfits and dress the family for a day outside. Comes in a magnetic closing &amp;#34;book&amp;#34; for safe keeping and easy storage.</long-description>
        <store-force-price-flag>false</store-force-price-flag>
        <store-non-inventory-flag>false</store-non-inventory-flag>
        <store-non-revenue-flag>false</store-non-revenue-flag>
        <store-non-discountable-flag>false</store-non-discountable-flag>
        <online-flag>true</online-flag>
        <online-from>2017-01-01T05:00:00.000Z</online-from>
        <available-flag>true</available-flag>
        <searchable-flag>false</searchable-flag>
        <tax-class-id>standard</tax-class-id>
        <brand>Janod</brand>
        <manufacturer-name>Juratoys Company</manufacturer-name>
        <sitemap-included-flag site-id="rco-us">true</sitemap-included-flag>
        <sitemap-changefrequency site-id="rco-us">weekly</sitemap-changefrequency>
        <sitemap-priority site-id="rco-us">1.0</sitemap-priority>
        <page-attributes/>
        <custom-attributes>
        <custom-attribute attribute-id="ID">6339080</custom-attribute>

How do i load that ID field into SQL? I tried this: But i believe my cross apply c.nodes is wrong? What Markups should i use? Thanks

SELECT a.id.query('id').value('.','varchar(50)') as id FROM 
( SELECT CAST(C AS XML) FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c)  ) AS S(c)
cross apply c.nodes('product/id') as A(id)

You ca try this:

SELECT c.id.query('id').value('.','varchar(50)') as id FROM 
( SELECT CAST(C AS XML) FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c)  ) AS T(c)
cross apply c.nodes('product/id') as c(id)

But in your shared xml couldn't find an id tag within product tag.

Here I am sharing an Example:

XML File content:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer>
    <Document>000 000 000</Document>
    <Name>Mary Angel</Name>
    <Address>Your City, YC 1212</Address>
    <Profession>Systems Analyst</Profession>
  </Customer>
  <Customer>
    <Document>000 000 001</Document>
    <Name>John Lenon</Name>
    <Address>Your City, YC 1212</Address>
    <Profession>Driver</Profession>
  </Customer>
  <Customer>
    <Document>000 000 002</Document>
    <Name>Alice Freeman</Name>
    <Address>Your City, YC 1212</Address>
    <Profession>Architect</Profession>
  </Customer>
  <Customer>
    <Document>000 000 003</Document>
    <Name>George Sands</Name>
    <Address>Your City, YC 1212</Address>
    <Profession>Doctor</Profession>
  </Customer>
  <Customer>
    <Document>000 000 004</Document>
    <Name>Mark Oliver</Name>
    <Address>Your City, YC 1212</Address>
    <Profession>Writer</Profession>
  </Customer>
</Customers>

Copy above content and save as a xml file in d drive named testxmlfile.xml

Then try below query:

SELECT
   MY_XML.Customer.query('Document').value('.', 'VARCHAR(20)') Document,
   MY_XML.Customer.query('Name').value('.', 'VARCHAR(50)') Name,
   MY_XML.Customer.query('Address').value('.', 'VARCHAR(50)')Address,
   MY_XML.Customer.query('Profession').value('.', 'VARCHAR(50)' )Profession
FROM (SELECT CAST(MY_XML AS xml)
      FROM OPENROWSET(BULK 'd:\testxmlfile.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
      CROSS APPLY MY_XML.nodes('Customers/Customer') AS MY_XML (Customer);

Output:

Document    Name            Address             Profession
000 000 000 Mary Angel      Your City, YC 1212  Systems Analyst
000 000 001 John Lenon      Your City, YC 1212  Driver
000 000 002 Alice Freeman   Your City, YC 1212  Architect
000 000 003 George Sands    Your City, YC 1212  Doctor
000 000 004 Mark Oliver     Your City, YC 1212  Writer

You haven't been clear about what exactly you want, but it looks like you want the attribute product-id from here:

    <product product-id="0217328320-sso-us">

You haven't taken into account namespaces, you need to add that.

It's also unclear if you have multiple product nodes. If you do, then you need this

WITH XMLNAMESPACES (DEFAULT 'http://www.demandware.com/xml/impex/catalog/2006-10-31')
SELECT A.product.value('@product-id','varchar(50)') as id
FROM 
( SELECT CAST(C AS XML)
  FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c)
) AS S(c)
cross apply S.c.nodes('/catalog/product') as A(product)

If you have only one product node then you don't need .values at all:

WITH XMLNAMESPACES (DEFAULT 'http://www.demandware.com/xml/impex/catalog/2006-10-31')
SELECT S.c.value('/catalog/product/@product-id','varchar(50)') as id FROM 
FROM 
( SELECT CAST(C AS XML)
  FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c)
) AS S(c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM