简体   繁体   English

SSIS用于创建XML文件

[英]SSIS issuse with creating XML file

Can you please tell me what is the best way to get the XML file with the Tile in the first row as: 您能告诉我第一行中带有Tile的XML文件的最佳方法是:

<?xml version="1.0" encoding="ISO-8859-15" ?>

and then the data rows? 然后是数据行?

The SQL query I am using is at the end of this post. 我正在使用的SQL查询在本文的结尾。

SSIS is some thing like this: SSIS是这样的:

  1. Execute SQL with the query attached and mapping the result set to a variable. 执行带有附加查询的SQL,并将结果集映射到变量。
  2. Using Script task for adding the Title in the first row. 使用脚本任务在第一行中添加标题。

I guess in the SQL, when I comment these two rows, the SQL runs and converts the output to XML: 我猜在SQL中,当我注释这两行时,SQL运行并将输出转换为XML:

    depgate as DepartingGate,
    arrgate as ArrivalGate,

If I dont comment the above rows, I am getting an error. 如果我不对以上各行发表评论,则会出现错误。 The error message I am getting is: 我收到的错误消息是:

Msg 9420, Level 16, State 1, Line 4 XML parsing: line 1, character 439, illegal xml character. 消息9420,级别16,状态1,第4行XML解析:第1行,字符439,非法xml字符。

I tried restricting the & and - , etc. but it didn't work. 我尝试限制&-等,但是没有用。

I saw the table and it is having some of the illegal characters for XML, but I am not sure how can I restrict these characters. 我看到了该表,其中包含XML的一些非法字符,但是我不确定如何限制这些字符。 Can you tell me what should be a good fix for this? 您能告诉我什么是一个好的解决方案吗?

 DECLARE @XmlOutput XML
    set @XmlOutput = (
    select
    sched_local_fltdate as FlightDate,
    flt_num as FlightNumber,
    Dep as Origin,
    Arr as Destination,
    ltrim(rtrim(substring(ac, 2, 10))) as AccountNumber,
    ac as RegisttrationNumber,
    actype,
    CASE WHEN cancelled = 'Y' then 'Cancelled'
    when flight_type = 'S' then 'Scheduled'
    when flight_type = 'O' then 'Spare Block'
    when flight_type = 'C' then 'Charter'
    when flight_type = 'F' then 'Ferry'
    when flight_type = 'M' then 'Military'
    when flight_type = 'SR' then 'Re-route'
    when flight_type = 'X' then 'Maintenance Block'
    else LTRIM(RTRIM(flight_type))
    end as FlightTypeDescription, 
    case when cancelled = 'Y' then 'Cancelled'
        when VPART_BLON > 0 then 'Arrived'
        when flight_type in ('O','X') then 'No Flight'
    end as FlightStatusDescription,
    out_utc as OutUTMS,
    in_utc as InUTMS,
    depgate as DepartingGate,
    arrgate as ArrivalGate,
    departure_fuel_lbs as DepartureFuelLBS,
    arrival_fuel_lbs as ArrivalFuelLBS,
    'N/A' as FuelUnitOfMSRName
    from [dbo].[ufn_Get_Flight_Operations_Base_Dataset] (cast(dateadd(day, -2, getdate()) as date), cast(getdate() as date), DEFAULT, DEFAULT, DEFAULT)

    for xml Path('row'), elements)

    Select @XmlOutput as XMLValue

You could write a fairly simple encoding function to escape the 5 characters XML is fussy about, then run those fields through the function. 您可以编写一个相当简单的编码函数来转义XML繁琐的5个字符,然后通过该函数运行这些字段。 Or use chained calls to REPLACE. 或使用链接调用来替换。 The XML consumer would need to properly handle the escaped characters. XML使用者需要正确处理转义的字符。

Here's one we did a while back for escaping the sensitive characters in large XML docs for one of our clients. 这是我们前一段时间为大型客户中的一个客户转义大型XML文档中的敏感字符而做的。

CREATE FUNCTION dbo.uf_XmlEncode (@as_input NVARCHAR(4000))
    RETURNS NVARCHAR(4000)
    WITH EXECUTE AS CALLER
AS

BEGIN

DECLARE @ls_text NVARCHAR(4000)

SET @ls_text = @as_input

SELECT @ls_text = REPLACE( @ls_text , '&', '&amp;' )
SELECT @ls_text = REPLACE( @ls_text , '''', '&apos;')
SELECT @ls_text = REPLACE( @ls_text , '"', '&quot;')
SELECT @ls_text = REPLACE( @ls_text , '>', '&gt;'  )
SELECT @ls_text = REPLACE( @ls_text , '<', '&lt;'  )

    RETURN COALESCE (@ls_text, @as_input)
END
GO

Decoding is the reverse. 解码是相反的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM