如何使用 karate.read 和 karate.write 保留文件编码

How to retain file encoding using karate.read and karate.write

我试图重写一个最初编码为 ISO-8859-1 的 XML 文件。 当我尝试打印输出时,它给了我乱码。有人可以帮助解决这个问题吗?

someFile.xml(确保文件以 ISO-8859-1 编码保存)

<?xml version="1.0" encoding="ISO-8859-1"?>
<text>å bestå gjennom fem århundrer også tålt</text>

test.feature

* def xmlData = karate.read("someFile.xml")
* karate.log(karate.prettyXml(xmlData))

输出:

<text>� best� gjennom fem �rhundrer ogs� t�lt</text>

我已确定我的 pom.xml 使用的是 ISO-8859-1 编码

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <argLine>-Dfile.encoding=ISO-8859-1</argLine>
  </configuration>
</plugin>

内部空手道偏向于 UTF-8。到目前为止,这还不是问题。

我建议两个选项:

  • 请为空手道贡献代码以改进它
  • 编写一个自定义 Java 实用程序,完全按照您想要的方式进行读取和写入,也许如果您在代码运行后共享该代码,我们可以考虑将这部分作为空手道的“核心”。请参考文档中的Java interop

另见: