解析xml文件并使用存储过程获取节点的父名称

时间:2013-02-08 04:27:34

标签: sql sql-server xml sql-server-2008

我有一个xml文件,它是SQL Server中存储过程的输入。我有一个包含element name列和parent id列的表格。根元素父标识为0,第一个元素父标识为1,依此类推。我如何实现这一目标?

我的xml中有几种复杂的类型

<VoyageOrderMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="voyage.xsd">
    <MessageHeader>
            <MessageId>ID</MessageId>
            <MessageDate>2009-11-01T11:42:07.414+03:00</MessageDate>
            <MessageTypeVersion>Version</MessageTypeVersion>
            <SenderId>SI</SenderId>
            <ReceiverDetails>
                <ReceiverMethod />
                <ReceiverFormat />
                <ReceiverAddress></ReceiverAddress>
                </ReceiverDetails>
    </MessageHeader>
    <VoyageOrder>
            <VoyageID>RG-FUW-001</VoyageID>
            <Amendment>4</Amendment>
            <IMO>9256200</IMO>
            <VesselName>Fuwairit</VesselName>
            <ShipMasterName />
            <OrderDate>2009-11-01T11:41:59.149+03:00</OrderDate>
            <Passage>
                <PassageNumber>1</PassageNumber>
                <PassageType>Laden</PassageType>
                <DeparturePortName>Ras Laffan</DeparturePortName>
                <DeparturePortCode>RLF</DeparturePortCode>
                <DepartureTime>2009-10-06T19:06:00.000+03:00</DepartureTime>
                <ArrivalPortName>Suez</ArrivalPortName>
                <ArrivalPortCode>SUZ</ArrivalPortCode>
                <ArrivalTime>2009-10-13T03:00:00.000+02:00</ArrivalTime>
            </Passage>

示例输出

elementname parent id column
------------------------------
voyageorder   0             1
messageheader 1           2
messageid     2           3
etc

感谢任何帮助

2 个答案:

答案 0 :(得分:0)

回答问题作者的第一个评论

CREATE TABLE Blah
(SomeData XML)

INSERT Blah
SELECT MyXMLinfo

答案 1 :(得分:0)

看起来OPENXML返回的边缘表格式是您可以使用的。

SQL Fiddle

MS SQL Server 2012架构设置

查询1

declare @xml xml = '
<VoyageOrderMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="voyage.xsd">
    <MessageHeader>
            <MessageId>ID</MessageId>
            <MessageDate>2009-11-01T11:42:07.414+03:00</MessageDate>
    </MessageHeader>
    <VoyageOrder>
            <VoyageID>RG-FUW-001</VoyageID>
            <Amendment>4</Amendment>
            <OrderDate>2009-11-01T11:41:59.149+03:00</OrderDate>
            <Passage>
                <PassageNumber>1</PassageNumber>
                <PassageType>Laden</PassageType>
            </Passage>
    </VoyageOrder>
</VoyageOrderMessage>'

declare @idoc int

exec sp_xml_preparedocument @idoc output, @xml

select *
from openxml(@idoc, '*')

exec sp_xml_removedocument @idoc

<强> Results

| ID | PARENTID | NODETYPE |                 LOCALNAME | PREFIX |                              NAMESPACEURI | DATATYPE |   PREV |                                      TEXT |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|  0 |   (null) |        1 |        VoyageOrderMessage | (null) |                                    (null) |   (null) | (null) |                                    (null) |
|  2 |        0 |        2 |                       xsi |  xmlns |                                    (null) |   (null) | (null) |                                    (null) |
| 14 |        2 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) | http://www.w3.org/2001/XMLSchema-instance |
|  3 |        0 |        2 | noNamespaceSchemaLocation |    xsi | http://www.w3.org/2001/XMLSchema-instance |   (null) | (null) |                                    (null) |
| 15 |        3 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |                                voyage.xsd |
|  4 |        0 |        1 |             MessageHeader | (null) |                                    (null) |   (null) | (null) |                                    (null) |
|  5 |        4 |        1 |                 MessageId | (null) |                                    (null) |   (null) | (null) |                                    (null) |
| 16 |        5 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |                                        ID |
|  6 |        4 |        1 |               MessageDate | (null) |                                    (null) |   (null) |      5 |                                    (null) |
| 17 |        6 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |             2009-11-01T11:42:07.414+03:00 |
|  7 |        0 |        1 |               VoyageOrder | (null) |                                    (null) |   (null) |      4 |                                    (null) |
|  8 |        7 |        1 |                  VoyageID | (null) |                                    (null) |   (null) | (null) |                                    (null) |
| 18 |        8 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |                                RG-FUW-001 |
|  9 |        7 |        1 |                 Amendment | (null) |                                    (null) |   (null) |      8 |                                    (null) |
| 19 |        9 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |                                         4 |
| 10 |        7 |        1 |                 OrderDate | (null) |                                    (null) |   (null) |      9 |                                    (null) |
| 20 |       10 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |             2009-11-01T11:41:59.149+03:00 |
| 11 |        7 |        1 |                   Passage | (null) |                                    (null) |   (null) |     10 |                                    (null) |
| 12 |       11 |        1 |             PassageNumber | (null) |                                    (null) |   (null) | (null) |                                    (null) |
| 21 |       12 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |                                         1 |
| 13 |       11 |        1 |               PassageType | (null) |                                    (null) |   (null) |     12 |                                    (null) |
| 22 |       13 |        3 |                     #text | (null) |                                    (null) |   (null) | (null) |                                     Laden |