我正在尝试使用节点从XML中读取所有项目,但是我设法仅返回第一个,或者在以下示例中都不返回。
如何阅读整个列表?
文件可以包含数十万个文件。
XML文件
<data source="1" target="0">
<list item_nb="1">
<co id="1" constitution="20190612101007" item_nb="44046">
<item cm="640001000101" obu="00007E" vc="2" vrn="SOLD" />
<item cm="640001000101" obu="00009D" vc="6" vrn="04D11797" />
<item cm="640001000101" obu="0000A3" vc="2" vrn="FAULTY" />
<item cm="640001000101" obu="00018B" vc="2" vrn="07D54084" />
<item cm="640001000101" obu="0001A6" vc="6" vrn="000422" />
<item cm="640001000101" obu="0001B2" vc="2" vrn="90D24430" />
<item cm="640001000101" obu="0001B3" vc="2" vrn="03LS1592" />
<item cm="640001000101" obu="0001B9" vc="6" vrn="FAULTYJUNE15" />
</co>
</list>
</data>
我在SQL中的代码(仅返回第一项)
declare @X xml;
select @X = T.MY_XML
from openrowset(bulk 'C:\XML\IEA.1.20190612101007-WL.XML', single_blob) as T(MY_XML)
select
MY_XML.Item.value('(item/@cm)[1]', 'VARCHAR(20)'),
MY_XML.Item.value('(item/@obu)[1]', 'VARCHAR(50)'),
MY_XML.Item.value('(item/@vc)[1]', 'VARCHAR(50)'),
MY_XML.Item.value('(item/@vrn)[1]', 'VARCHAR(50)')
from @X.nodes('data/list/co') AS MY_XML (Item);
答案 0 :(得分:1)
找到一种工作方式!
declare @X xml;
select @X = T.MY_XML
from openrowset(bulk 'C:\XML\IEA.1.20190612101007-WL.XML', single_blob) as T(MY_XML)
select
MY_XML.Item.value('(@cm)[1]', 'VARCHAR(20)'),
MY_XML.Item.value('(@obu)[1]', 'VARCHAR(50)'),
MY_XML.Item.value('(@vc)[1]', 'VARCHAR(50)'),
MY_XML.Item.value('(@vrn)[1]', 'VARCHAR(50)')
from @X.nodes('data/list/co/item') AS MY_XML (Item);