Norman W. Freeman · Dec 24, 2020

Is there a way to automatically remove some specific characters from the response of a web service ?

I have implemented a web service that inherit from %SOAP.WebService

It exposes classes with string properties : 

Class Employee Extends (%RegisteredObject, %XML.Adaptor) [ ProcedureBlock ]

Parameter XMLNAME = "Employee";
Parameter XMLSEQUENCE = 1;

Property FirstName As %String(MAXLEN = "", XMLNAME = "FirstName") [ Required ];
Property LastName As %String(MAXLEN = "", XMLNAME = "LastName") [ Required ];

Here is the issue: those properties are filled from a huge Caché database which contains forbidden XML characters (usually control characters in the 0-31 range).

Ideally, the database would be cleaned, but this is something not possible here.
My current solution is to manually clean the text data before properties are set : 

set employee.FirstName = ##class(Utils).RemoveSpecialChars(...) //this function call $ZSTRIP(...,"*C")
set employee.LastName = ##class(Utils).RemoveSpecialChars(...)

It works, but it is cumbersome and make source code harder to read than it should be.

I was wondering if there would be a better to achieve same result, but without the need to explicitly do this for all text properties.

What I have in mind (I have not been able achieve it) :

- To get the whole XML response (eg: in a %SOAP.WebService callback) before it is send back to the client, and remove invalid characters from it.
- To implement a custom %XML.Writer and give it to the web service. That writer will take care of calling $ZSTRIP() before outputting text in XML nodes.


2 0 1 64