【问题标题】:XmlBeans generates Java source files with ANSI encodingXmlBeans 生成带有 ANSI 编码的 Java 源文件
【发布时间】:2014-06-27 07:23:13
【问题描述】:

我正在使用 XmlBeans 2.6.0 编译一些包含希腊单词枚举的 XSD 文件:

<xs:simpleType name="t_series_report">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Γενική"/>
        <xs:enumeration value="Ειδική"/>
    </xs:restriction>
</xs:simpleType>

使用包含在 XmlBeans 的 ZIP 二进制分发的 xbean.jar 中的 Ant 任务执行编译。 XSD 文件保存为 utf-8,并且在标头 java 文件中也正确说明了这一点

<?xml version="1.0" encoding="UTF-8"?>

问题是 XmlBeans 生成的 Java 文件似乎以 ANSI 字符集保存,并且在编译过程中出现如下错误:

  [xmlbean] C:\projects\myproject\workspace\prj\build\xmlbeans\test\src\com\company\project\schema\myschematype\cl\cle\ext\TMyType.java:61: illegal character: \8220
  [xmlbean]         static final int INT_ΓΕ�?ΙΚΉ = 1;
  [xmlbean]   

有没有办法强制 XmlBeans 将生成的 Java 文件保存为 UTF-8 而不是 ANSI?

【问题讨论】:

  • 您是作为 Maven 插件还是通过命令行 (scomp) 调用 XMLBeans?
  • @Gyro Gearless:作为蚂蚁任务

标签: java utf-8 xsd xmlbeans


【解决方案1】:

我们在使用 XMLBeans 的 maven 任务编译一些包含希腊语“Omega”的模式时遇到了类似的问题。

问题是,XMLBeans(至少从 2.5.0 版开始)总是使用 Java 平台默认编码,只能通过使用 -Dfile.encoding=UTF-8 调用 JVM 来设置。

对于我们的 Maven 项目,解决方案是不使用插件;相反,我们使用 exec 插件调用 XMLBeans,因此我们可以控制编码。这是pom.xml的sn-p

<plugin>
            <groupId>org.codehaus.mojo</groupId> 
            <artifactId>exec-maven-plugin</artifactId>
            <executions>

                <execution>
                    <id>exec-2.1.0</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>-Dfile.encoding=${project.build.sourceEncoding}</argument>
                            <argument>-classpath</argument>
                            <!-- automatically creates the classpath using all project dependencies,
                            also adding the project build directory -->
                            <classpath/>
                            <argument>org.apache.xmlbeans.impl.tool.SchemaCompiler</argument>
                            <argument>-src</argument>
                            <argument>${project.build.directory}/generated-sources</argument>
                            <argument>-srconly</argument>
                            <argument>-d</argument>
                            <argument>${project.build.directory}/classes</argument>
                            <argument>-javasource</argument>
                            <argument>1.6</argument>
                            <argument>${basedir}/src/main/2.1.0/schema/</argument>
                            <argument>src/main/2.1.0/config/FooBar_v2.1.0.xsdconfig</argument>
                        </arguments>
                    </configuration>
                </execution>

我想这种方法也适用于Ant

更简单的解决方案是像这样调用 ant:

ant -Dfile.encoding=UTF-8 build-or-whatever

但这显然只有在您的所有源文件都是 UTF-8 格式时才有效!

【讨论】:

  • 谢谢,ant -Dfile.encoding=UTF-8 build-xmlbeans 成功了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
相关资源
最近更新 更多