0

我正在使用 SELECT ... FOR XML 来生成 XML,它创建的正是我想要看到的 -在 SSMS 中。但...

我遇到的问题是,导出的 XML 文件将整个 XML 正文放在一行上。它是有效的 XML,但如何格式化输出?(每行一个元素,以 \r\n 结尾,最好缩进)该文件应该在记事本中可读。

另外,是否有更好的方法来附加 XML 标头?

当然,我可以编写一个 CLR 存储过程来进行导出,但这似乎有点矫枉过正。(为什么这更难?)

ALTER PROCEDURE [dbo].[ExportConfigTablesToXML] 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

--EXEC sp_configure 'show advanced options', 1
--RECONFIGURE
--EXEC sp_configure 'xp_cmdshell', 1
--RECONFIGURE
--EXEC sp_configure 'show advanced options', 0
--RECONFIGURE

DECLARE @FileName VARCHAR(50)
DECLARE @SQLCmd   VARCHAR(500)
DECLARE @CreateXmlHeader varchar(500)
DECLARE @AppendXmlBody varchar(500)
DECLARE @DeleteTempFile varchar(500)

SELECT @FileName = 'C:\Temp\SampleXMLOutput.xml'
SELECT @SQLCmd = 'bcp ' + 
    '"SELECT Name AS [@Name], ServiceName, MachineName,' +
    '    CASE PollInterval WHEN 10 THEN NULL ELSE PollInterval END AS PollInterval,' +
    '    CASE Alerts WHEN 1 THEN NULL ELSE ''false'' END AS Alerts, ' +
    '    CASE Pages WHEN 1 THEN NULL ELSE ''false'' END AS Pages' +
    ' FROM Watchdog.dbo.[Config.Services] [Service]' +
    ' FOR XML PATH(''Service''), ROOT(''Services'')"' +
    ' queryout ' +
    @FileName + '.tmp' +
    ' -S' + @@SERVERNAME +
    ' -T -c -r -t'

SET @CreateXmlHeader = 'ECHO ^<?xml version="1.0" ?^> > ' + @FileName
SET @AppendXmlBody = 'TYPE ' + @FileName + '.tmp >> ' + @FileName
SET @DeleteTempFile = 'DEL ' + @FileName + '.tmp'

EXECUTE master..xp_cmdshell @SQLCmd
EXECUTE master..xp_cmdshell @CreateXmlHeader
EXECUTE master..xp_cmdshell @AppendXmlBody
EXECUTE master..xp_cmdshell @DeleteTempFile

END
4

3 回答 3

1

您可以通过将 XML 提取放入子查询中来添加标头。所以你的 BCP 命令变成:

SELECT @SQLCmd = 'bcp ' +  
    '"SELECT ''<?xml version="1.0"?>'' + ' +
    '(SELECT Name AS [@Name], ServiceName, MachineName,' + 
    '    CASE PollInterval WHEN 10 THEN NULL ELSE PollInterval END AS PollInterval,' + 
    '    CASE Alerts WHEN 1 THEN NULL ELSE ''false'' END AS Alerts, ' + 
    '    CASE Pages WHEN 1 THEN NULL ELSE ''false'' END AS Pages' + 
    ' FROM Watchdog.dbo.[Config.Services] [Service]' + 
    ' FOR XML PATH(''Service''), ROOT(''Services''))"' + 
    ' queryout ' + 
    @FileName + '.tmp' + 
    ' -S' + @@SERVERNAME + 
    ' -T -c -r -t' 
于 2010-08-15T18:09:22.430 回答
1

为了结束循环,我确实找到了一种解决方案,它每行写入一个 XML 标记,没有缩进。这是一个令人作呕的 hack,但我的目的是可以接受的,让我们看看它是否通过了代码审查。如果有人想查看带有缩进的文件,他们可以在 IE 中打开它(通过双击它)。

当然,真正的解决方案是 CLR 存储过程,但现在我更喜欢这种方式以保持部署简单,即只运行一个 SQL 脚本来部署数据库。

ALTER PROCEDURE [dbo].[ExportConfigTablesToXML] 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

--EXEC sp_configure 'show advanced options', 1
--RECONFIGURE
--EXEC sp_configure 'xp_cmdshell', 1
--RECONFIGURE
--EXEC sp_configure 'show advanced options', 0
--RECONFIGURE

DECLARE @FileName VARCHAR(50)
DECLARE @SQLCmd   VARCHAR(1000)

SELECT @FileName = 'C:\Temp\SampleXMLOutput.xml'
SELECT @SQLCmd = 'bcp ' + 
    '"DECLARE @xml xml; ' +
    'DECLARE @text varchar(MAX); ' +

    'SET @xml = (SELECT Name AS [@Name], ServiceName, MachineName, ' +
    '   CASE PollInterval WHEN 10 THEN NULL ELSE PollInterval END AS PollInterval, ' +
    '   CASE Alerts WHEN 1 THEN NULL ELSE ''false'' END AS Alerts, ' +
    '   CASE Pages WHEN 1 THEN NULL ELSE ''false'' END AS Pages ' +
    'FROM Watchdog.dbo.[Config.Services] [Service] ' +
    'FOR XML PATH(''Service''), ROOT(''Services''), TYPE); ' +

    'SET @text = ''<?xml version=""1.0"" ?>'' + CHAR(13) + CHAR(10); ' +
    'SET @text = @text + REPLACE(CAST(@xml AS varchar(MAX)), ''><'', ''>'' + CHAR(13) + CHAR(10) + ''<''); ' +
    'SELECT @text" ' +

    ' queryout ' +
    @FileName + 
    ' -S' + @@SERVERNAME +
    ' -T -c -r -t'

EXECUTE master..xp_cmdshell @SQLCmd

END
于 2010-08-16T19:28:53.080 回答
0

由于无论如何您都在运行命令,您也许可以:

xmllint --format

来自免费的 XML 格式化工具

于 2010-08-14T19:17:16.997 回答