Encoding XML carriage return / line feed
Hi
I'm using %XML.Writer to write XML files from a database source, and I see that it is not encoding carriage return or line feed as 
 and 
 respectively, and is instead leaving them unencode.
What can I do to have them encoded?
Andy
Comments
Please consider providing a code sample.
Yes, I can see that might help. I'm writing the following class to XML
Class OutputData Extends (%Persistent, %XML.Adaptor)
{
Parameter XMLNAME = "OutputData";
Property Notes As %String(MAXLEN = "", XMLNAME = "Notes", XMLPROJECTION = "ATTRIBUTE");
}Using the following code
#dim item as DATAEXTRACTION.Datix.Message.Fragment.NewClass1 set item = ##class(DATAEXTRACTION.Datix.Message.Fragment.NewClass1).%New() set item.Notes = "< > &" _$c(10)_$c(13)_ "TEST" #dim tWriter as %XML.Writer set tWriter=##class(%XML.Writer).%New() set tWriter.Indent=4 do tWriter.OutputToFile("E:\Testing\out.xml") set tSC=tWriter.RootObject(item)
Which produces the following XML, the $C(10) and $C(13) are not encoded, but the other characters are:
<?xml version="1.0" encoding="UTF-8"?> <OutputData Notes="< > & TEST"></OutputData>
And what I want is:
<?xml version="1.0" encoding="UTF-8"?> <OutputData Notes="< > & 
 
 TEST"></OutputData>
The reason for wanting this is that I'm building a new system to replace some legacy code and the data outputted by the new system has to be identical
Just checked XML spec and there's nothing about escaping $c(10) or $c(13).
The only symbols which must be escaped are:
- " "
- & &
- ‘ '
- ' '
- < <
- > >
You can check $zcvt - it produces the same output:
zw $zcvt("< > &" _$c(10)_$c(13)_ "TEST", "O", "XML")If you need byte for byte compatibility you'll need a custom datatype with a redefined LogicalToXSD method. Something like:
Class test.XMLString Extends %String
{
ClassMethod LogicalToXSD(%val As %String) As %String [ CodeMode = objectgenerator, ServerOnly = 1 ]
{
quit:%mode'="propertymethod" $$$OK
Do %code.WriteLine($c(9) _ "set %val = $zcvt(%val,""O"",""XML"")")
For replace=$lb("$c(10)","""
"""),$lb("$c(13)","""
""") {
Set from = $lg(replace, 1)
Set to = $lg(replace, 2)
Do %code.WriteLine($c(9) _ "set %val = $replace(%val," _ from _ ", " _ to _")")
}
Do %code.WriteLine($c(9) _ "quit %val")
Quit $$$OK
}
}