简体   繁体   English

选择以libxml2结尾的行

[英]Choosing line ending with libxml2

I try to generate some xml files ( TMX ) on our servers. 我尝试在我们的服务器上生成一些xml文件( TMX )。

The servers are Solaris SPARC servers, but the destination of the files are some legacy Windows CAT Tools. 这些服务器是Solaris SPARC服务器,但是文件的目标是某些旧版Windows CAT工具。

The CAT-Tool requires CR+LF line endings as is the default on Windows. CAT-Tool需要CR+LF线尾,这是Windows上的默认设置。 Writing the files with libxml2, using xmlWriter is easy and works quite well. 使用xmlWriter使用libxml2编写文件很容易,并且效果很好。 But I haven't figured out a way to force the lib to emit CR+LF instead of the Unix standard LF . 但是我还没有找到一种方法来强制lib发出CR+LF而不是Unix标准LF The lib only seem to support the line ending of the platform it runs on. lib似乎仅支持其运行平台的行尾。

Has somebody found a way to generate files with another line ending than the default of the platform it runs on. 有没有人找到一种方法来生成另一行结尾的文件,而不是其运行平台的默认行。 Actually my workaround is to open the written file and writing a new file with the changed line ending using a simple C loop. 实际上,我的解决方法是打开写入的文件,然后使用简单的C循环以更改后的行结尾写入新文件。 That works, but it is annoying to have such a unnecessary step in our chain. 那行得通,但是在我们的链中如此不必要的步骤令人讨厌。

I haven't tried this myself, but from xmlsave , I can see two possibilities 我自己还没有尝试过,但是从xmlsave中 ,我可以看到两种可能性

  • xmlSaveToBuffer : save to a buffer, convert to CR/LF and write it out yourself. xmlSaveToBuffer :保存到缓冲区,转换为CR / LF并自己写出来。
  • xmlSaveToIO : register an iowrite callback and convert to CF/LF while writing in your callback function xmlSaveToIO :注册一个iowrite回调并在编写回调函数时转换为CF / LF

Maybe, there are other options, but I haven't found them. 也许还有其他选择,但我还没有找到。

The CAT-Tool requires CR+LF line endings as is the default on Windows. CAT-Tool需要CR + LF线尾,这是Windows上的默认设置。

FWIW, that means the CAT-Tool has a broken XML parser. FWIW,这意味着CAT工具具有损坏的XML解析器。 It shouldn't care about this, as the the XML spec says: 正如XML规范所述 ,它不必在乎这一点:

To simplify the tasks of applications, the XML processor must behave as if it normalized all line breaks ... by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character. 为了简化应用程序的任务,XML处理器必须像将所有换行符规范化一样运行...通过将两个字符序列#xD #xA和未跟在#xA之后的任何#xD转换为单个#xA字符。

I know often these things are out of our control, but if you can lean on the CAT-Tool vendor to fix their software, it could become a more future-proof solution. 我知道这些事情通常是我们无法控制的,但是如果您可以依靠CAT-Tool供应商来修复他们的软件,则它可能会成为更具前瞻性的解决方案。

According to the source code (as of April 2013), libxml2 just puts "\\n" into the output stream. 根据源代码(截至2013年4月),libxml2只是将“ \\ n”放入输出流。 At least, when writing dtd-part of a document. 至少在编写文档的dtd部分时。 Therefore, re-encoding the stream on the fly is the only option to get "\\r\\n" as result. 因此,动态重新编码流是获得“ \\ r \\ n”结果的唯一选择。

If you were lucky (as me) and your tool run on Windows, you could open the file in the text mode, and the OS would do recoding for you. 如果您很幸运(像我一样)并且您的工具在Windows上运行,则可以以文本模式打开文件,操作系统将为您重新编码。

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

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