用于 XML 的 SQL Server 2005
我想将数据从表导出到特定格式的 XML 文件。我对 XML 文件相当陌生,所以我所追求的可能是相当明显的,但我只是无法在网上找到我要寻找的内容。
我需要的 XML 结果的格式是:
<data>
<event
start="May 28 2006 09:00:00 GMT"
end="Jun 15 2006 09:00:00 GMT"
isDuration="true"
title="Writing Timeline documentation"
image="http://simile.mit.edu/images/csail-logo.gif">
A few days to write some documentation
</event>
</data>
我的表结构是:(
name VARCHAR(50),
description VARCHAR(255),
startDate DATETIME,
endDate DATETIME
此时我对 XML 字段 image 或 isDuration 不太感兴趣)。
我已经尝试过:
SELECT [name]
,[description]
,[startDate]
,[endTime]
FROM [testing].[dbo].[time_timeline]
FOR XML RAW('event'), ROOT('data'), type
这给了我:
<data>
<event name="Test1"
description="Test 1 Description...."
startDate="1900-01-01T00:00:00"
endTime="1900-01-01T00:00:00"
/>
<event name="Test2"
description="Test 2 Description...."
startDate="1900-01-01T00:00:00"
endTime="1900-01-01T00:00:00"
/>
</data>
我缺少的是描述需要位于事件属性之外,并且需要有一个标签。
有谁能够为我指出正确的方向,或者为我指出如何实现此目标的教程或类似内容?
谢谢,
马特
I want to export data from a table to a specifically formatted XML file. I am fairly new to XML files, so what I am after may be quite obvious but I just cant find what I am looking for on the net.
The format of the XML results I need are:
<data>
<event
start="May 28 2006 09:00:00 GMT"
end="Jun 15 2006 09:00:00 GMT"
isDuration="true"
title="Writing Timeline documentation"
image="http://simile.mit.edu/images/csail-logo.gif">
A few days to write some documentation
</event>
</data>
My table structure is:
name VARCHAR(50),
description VARCHAR(255),
startDate DATETIME,
endDate DATETIME
(I am not too interested in the XML fields image or isDuration at this point in time).
I have tried:
SELECT [name]
,[description]
,[startDate]
,[endTime]
FROM [testing].[dbo].[time_timeline]
FOR XML RAW('event'), ROOT('data'), type
Which gives me:
<data>
<event name="Test1"
description="Test 1 Description...."
startDate="1900-01-01T00:00:00"
endTime="1900-01-01T00:00:00"
/>
<event name="Test2"
description="Test 2 Description...."
startDate="1900-01-01T00:00:00"
endTime="1900-01-01T00:00:00"
/>
</data>
What I am missing, is the description needs to be outside of the event attributes, and there needs to be a tag.
Is anyone able to point me in the correct direction, or point me to a tutorial or similar on how to accomplish this?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以完成工作:
需要注意的事项:
description
作为event
的文本内容,我们必须“提升”一个级别并使用PATH('')
,并在所有列的别名中指定名称event
要学习这些内容(或在至少了解如何执行您想要的操作),请参阅
FOR XML
的文档,只需使用您自己的表和所需的 XML 结构即可。This should do the job:
Things to note:
description
as the text content ofevent
, we have to 'step up' a level and usePATH('')
, and specify the nameevent
in the alias for all columnsFor learning this stuff (or at least getting an idea of how to do what you want), see the docs for
FOR XML
and just play around with your own tables and desired XML structures.考虑切换到 FOR XML PATH(请参阅 http://msdn.microsoft.com/ en-us/library/ms189885.aspx 获取文档)
一个非常部分的示例:
Consider switching to FOR XML PATH (see http://msdn.microsoft.com/en-us/library/ms189885.aspx for documentation)
A very partial example: