IBM DOORs DXL字符串转义序列识别与移除方案咨询
解决DOORS导出CSV时的转义序列问题
一、通用字符串清理函数(移除/替换转义序列)
先实现一个DXL函数,专门清理字符串中的各类控制字符(包括换行、制表符、回车等转义序列),直接集成到你的脚本即可:
// 清理字符串中的所有控制字符(转义序列) string cleanEscapedChars(string inputStr) { string cleaned = inputStr // 替换换行符 cleaned = replace(cleaned, "\n", "") // 替换制表符 cleaned = replace(cleaned, "\t", "") // 替换回车符 cleaned = replace(cleaned, "\r", "") // 替换其他少见的控制字符 cleaned = replace(cleaned, "\v", "") cleaned = replace(cleaned, "\f", "") return cleaned }
如果不想完全移除转义序列,而是替换为可读标记(比如把换行换成[换行]),只需要将上述代码中的空字符串替换为对应标记即可。
二、优化后的完整导出脚本
将清理函数整合到导出逻辑中,同时优化列遍历方式(无需手动逐个定义列变量,用循环更易维护):
// 清理字符串中的所有控制字符(转义序列) string cleanEscapedChars(string inputStr) { string cleaned = inputStr cleaned = replace(cleaned, "\n", "") cleaned = replace(cleaned, "\t", "") cleaned = replace(cleaned, "\r", "") cleaned = replace(cleaned, "\v", "") cleaned = replace(cleaned, "\f", "") return cleaned } // 主导出逻辑 Stream file = write(exportFile); if (null file) { print "Error: Unable to create CSV file.\n"; close(modHandle); continue; } // 写入CSV表头 Column c int colCount = columns(current) for c in current do { file << title(c) // 最后一列不追加逗号 if (index(c) < colCount - 1) file << "," } file << "\n" // 遍历模块对象并导出数据 Object o for o in current do { // 处理第一列(ID) string cellValue = text(column(0), o) file << cellValue << "," // 处理其余列 for (int i = 1; i < colCount; i++) { cellValue = cleanEscapedChars(text(column(i), o)) // 遵循CSV标准:用双引号包裹字段 file << "\"" << cellValue << "\"" // 最后一列不追加逗号 if (i < colCount - 1) file << "," } file << "\n" } close(file); print "Export completed: " exportFile "\n";
三、识别未知转义序列的方法
如果上述替换仍未生效,说明可能存在未覆盖的控制字符,可通过以下脚本打印字符串中每个字符的ASCII码,精准定位问题字符:
// 打印字符串每个字符的ASCII码 void printCharCodes(string inputStr) { int len = length(inputStr) for (int i = 0; i < len; i++) { char ch = inputStr[i] int code = ascii(ch) print "位置" i ": 字符[" ch "] ASCII码:" code "\n" } } // 使用示例:传入有问题的列内容 Object testObj = first current string problematicStr = text(column(1), testObj) printCharCodes(problematicStr)
运行后查看DOORS日志窗口,ASCII码小于32的均为控制字符(转义序列),比如:
- 9 = 制表符(\t)
- 10 = 换行符(\n)
- 13 = 回车符(\r)
找到对应ASCII码后,在cleanEscapedChars函数中添加对应的替换规则即可。
内容的提问来源于stack exchange,提问作者Ethan Brown
相关产品推荐
相关产品推荐

