简体   繁体   中英

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:

<?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.

SSIS is some thing like this:

  1. Execute SQL with the query attached and mapping the result set to a variable.
  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:

    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.

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. 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. Or use chained calls to REPLACE. The XML consumer would need to properly handle the escaped characters.

Here's one we did a while back for escaping the sensitive characters in large XML docs for one of our clients.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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