XML到SQL - 包含没有不同标识符的列的行

时间:2015-12-21 21:17:57

标签: sql sql-server xml xml-parsing

将这种XML导入/转换为SQL数据库的正确,最有效的方法是什么:

<?xml version="1.0" encoding="UTF-8"?>
<data app-version-major="xyz" app-version-minor="xyz" app-version-build="xyz" schema-version="xyz" export-time="xyz" db-product-name="Apache Derby" db-product-version="xyz">
<table name="table1" entire="Y">
    <columns>
        <column name="col1" class="java.lang.Long" type-id="-5" db-type="bigint"/>
        <column name="col2" class="java.lang.String" length="10" type-id="12" db-type="varchar"/>
        <column name="col3" class="java.lang.String" length="255" type-id="12" db-type="varchar"/>
        <column name="col4" class="java.lang.Double" type-id="8" db-type="double"/>
        <column name="col5" class="java.lang.String" length="1" type-id="1" db-type="char"/>
        <column name="col6" class="java.lang.Double" type-id="8" db-type="double"/>
        <column name="col7" class="java.lang.String" length="50" type-id="12" db-type="varchar"/>
        <column name="col8" class="java.lang.String" length="1" type-id="1" db-type="char"/>
        <column name="col9" class="java.lang.String" length="2000" type-id="12" db-type="varchar"/>
        <column name="col11" class="java.lang.String" length="1" type-id="1" db-type="char"/>
        <column name="col12" class="java.sql.Timestamp" type-id="93" db-type="timestamp"/>
        <column name="col13" class="java.sql.Timestamp" type-id="93" db-type="timestamp"/>
        <column name="col14" class="java.lang.String" length="50" type-id="12" db-type="varchar"/>
        <column name="col15" class="java.sql.Timestamp" type-id="93" db-type="timestamp"/>
        <column name="col16" class="java.lang.String" length="50" type-id="12" db-type="varchar"/>
        <column name="col17" class="java.lang.Long" type-id="-5" db-type="bigint"/>
        <column name="col18" class="java.lang.String" length="255" type-id="12" db-type="varchar"/>
        <column name="col19" class="java.lang.String" length="255" type-id="12" db-type="varchar"/>
        <column name="col20" class="java.lang.String" length="255" type-id="12" db-type="varchar"/>
        <column name="col21" class="java.lang.String" length="1" type-id="1" db-type="char"/>
        <column name="col22" class="java.sql.Timestamp" type-id="93" db-type="timestamp"/>
        <column name="col23" class="java.lang.String" length="20" type-id="12" db-type="varchar"/>
        <column name="col24" class="java.lang.String" length="20" type-id="12" db-type="varchar"/>
        <column name="col25" class="java.lang.String" length="50" type-id="12" db-type="varchar"/>
        <column name="col26" class="java.lang.Long" type-id="-5" db-type="bigint"/>
    </columns>
    <row>
        <v>value1</v>
        <v>value2</v>
        <v>value3</v>
        <v>value4</v>
        <v>value5</v>
        <v>value6</v>
        <v>value7</v>
        <v>value8</v>
        <v>value9</v>
        <v>value10</v>
        <v>value11</v>
        <v>value12</v>
        <v>value13</v>
        <v>value14</v>
        <v>value15</v>
        <v>value16</v>
        <v>value17</v>
        <v>value18</v>
        <v>value19</v>
        <v>value20</v>
        <v>value21</v>
        <v>value22</v>
        <v>value23</v>
        <v>value24</v>
        <v>value25</v>
    </row>
    <row>
        <v>value1</v>
        <v>value2</v>
        <v>value3</v>
        <v>value4</v>
        <v>value5</v>
        <v>value6</v>
        <v>value7</v>
        <v>value8</v>
        <v>value9</v>
        <v>value10</v>
        <v>value11</v>
        <v>value12</v>
        <v>value13</v>
        <v>value14</v>
        <v>value15</v>
        <v>value16</v>
        <v>value17</v>
        <v>value18</v>
        <v>value19</v>
        <v>value20</v>
        <v>value21</v>
        <v>value22</v>
        <v>value23</v>
        <v>value24</v>
        <v>value25</v>
    </row>
</table>

当前的实验已经在SQL Express上进行,试图找到一种方法来获取<row>标记内的信息以纠正列(row1-&gt; col1-&gt; value1等)。

我已尝试按照本指南进行操作:https://www.mssqltips.com/sqlservertip/2899/importing-and-processing-data-from-xml-files-into-sql-server-tables/ - 但我仍然无法选择&#34;匿名&#34;行内的列数据 - 我得到的只是Null。

我还试图运行一些XML到CSV转换器而没有运气。这会是什么方式?

2 个答案:

答案 0 :(得分:0)

xml的好处是你可以按行分别拥有5,10或100个值。同样的过程可以适用。

使用xml解析器(java或其他语言),并迭代

每行

,用SQL注入。

即使对于数以千计的数据也是如此。

答案 1 :(得分:0)

如果您的XML在结构上始终相同,我会这样做:

在我的例子中,我假设XML已经被读入XML变量。如果您需要帮助如何将XML从文件读入SQL,请询问...

只需将其粘贴到空查询窗口并执行即可。适应您的需求......

DECLARE @XML XML=
'<data app-version-major="xyz" app-version-minor="xyz" app-version-build="xyz" schema-version="xyz" export-time="xyz" db-product-name="Apache Derby" db-product-version="xyz">
  <table name="table1" entire="Y">
    <columns>
      <column name="col1" class="java.lang.Long" type-id="-5" db-type="bigint" />
      <column name="col2" class="java.lang.String" length="10" type-id="12" db-type="varchar" />
      <column name="col3" class="java.lang.String" length="255" type-id="12" db-type="varchar" />
      <column name="col4" class="java.lang.Double" type-id="8" db-type="double" />
      <column name="col5" class="java.lang.String" length="1" type-id="1" db-type="char" />
      <column name="col6" class="java.lang.Double" type-id="8" db-type="double" />
      <column name="col7" class="java.lang.String" length="50" type-id="12" db-type="varchar" />
      <column name="col8" class="java.lang.String" length="1" type-id="1" db-type="char" />
      <column name="col9" class="java.lang.String" length="2000" type-id="12" db-type="varchar" />
      <column name="col11" class="java.lang.String" length="1" type-id="1" db-type="char" />
      <column name="col12" class="java.sql.Timestamp" type-id="93" db-type="timestamp" />
      <column name="col13" class="java.sql.Timestamp" type-id="93" db-type="timestamp" />
      <column name="col14" class="java.lang.String" length="50" type-id="12" db-type="varchar" />
      <column name="col15" class="java.sql.Timestamp" type-id="93" db-type="timestamp" />
      <column name="col16" class="java.lang.String" length="50" type-id="12" db-type="varchar" />
      <column name="col17" class="java.lang.Long" type-id="-5" db-type="bigint" />
      <column name="col18" class="java.lang.String" length="255" type-id="12" db-type="varchar" />
      <column name="col19" class="java.lang.String" length="255" type-id="12" db-type="varchar" />
      <column name="col20" class="java.lang.String" length="255" type-id="12" db-type="varchar" />
      <column name="col21" class="java.lang.String" length="1" type-id="1" db-type="char" />
      <column name="col22" class="java.sql.Timestamp" type-id="93" db-type="timestamp" />
      <column name="col23" class="java.lang.String" length="20" type-id="12" db-type="varchar" />
      <column name="col24" class="java.lang.String" length="20" type-id="12" db-type="varchar" />
      <column name="col25" class="java.lang.String" length="50" type-id="12" db-type="varchar" />
      <column name="col26" class="java.lang.Long" type-id="-5" db-type="bigint" />
    </columns>
    <row>
      <v>value1</v>
      <v>value2</v>
      <v>value3</v>
      <v>value4</v>
      <v>value5</v>
      <v>value6</v>
      <v>value7</v>
      <v>value8</v>
      <v>value9</v>
      <v>value10</v>
      <v>value11</v>
      <v>value12</v>
      <v>value13</v>
      <v>value14</v>
      <v>value15</v>
      <v>value16</v>
      <v>value17</v>
      <v>value18</v>
      <v>value19</v>
      <v>value20</v>
      <v>value21</v>
      <v>value22</v>
      <v>value23</v>
      <v>value24</v>
      <v>value25</v>
    </row>
    <row>
      <v>value1</v>
      <v>value2</v>
      <v>value3</v>
      <v>value4</v>
      <v>value5</v>
      <v>value6</v>
      <v>value7</v>
      <v>value8</v>
      <v>value9</v>
      <v>value10</v>
      <v>value11</v>
      <v>value12</v>
      <v>value13</v>
      <v>value14</v>
      <v>value15</v>
      <v>value16</v>
      <v>value17</v>
      <v>value18</v>
      <v>value19</v>
      <v>value20</v>
      <v>value21</v>
      <v>value22</v>
      <v>value23</v>
      <v>value24</v>
      <v>value25</v>
    </row>
  </table>
</data>';

SELECT @XML.value('/data[1]/@app-version-major','varchar(max)') AS AppVersionMajor
      ,@XML.value('/data[1]/@app-version-minor','varchar(max)') AS AppVersionMinor 
      --further attribs of node "data"
      ,@XML.value('(/data/table)[1]/@name','varchar(max)') AS TableName 
      ,@XML.value('(/data/table)[1]/@entire','varchar(max)') AS TableEntire

      ,One.Row.value('v[1]','varchar(max)') AS col1 --use the fitting name and type of your "table" node here
      ,One.Row.value('v[2]','varchar(max)') AS col2
      ,One.Row.value('v[3]','varchar(max)') AS col3
      --further columns here

FROM @XML.nodes('/data/table/row') AS One(Row)