java - 序言中不允许出现内容 - Unix 中发生异常,但使用相同代码的 Windows 中则不会发生异常

标签 java parsing unix dom sax

我已经在这个问题上苦苦挣扎了一个多星期了。我可能阅读了超过 50 个不同的页面,但我找不到适合我的情况的解决方案。

当然,如果没有一个特定的点,我的问题就会显得重复:我的代码确实可以在 Windows 中运行,并且相同的代码在 Unix 中运行时会导致此主题出现问题。

基本上,在论坛中进行的所有搜索都让我明白这是 BOM 的问题。我遵循了所有建议,我的代码在 Windows 中继续工作,但在 Unix 大型机中导致了同样的问题。

在下面查找我的代码中最相关的步骤,并评论我尝试过的尝试。很难想象还有什么可做的,因为从一开始我的代码就在 Windows 中运行,但仅在 Unix 大型机中导致 Cotent 问题

第一步:将文件序列化为 DOM 对象

Element txns = q.parseMHEFile(path to my file);

DOMImplementationLS lsImpl = (DOMImplementationLS) txns.getOwnerDocument().getImplementation().getFeature("LS", "3.0");

LSSerializer serializer = lsImpl.createLSSerializer();
serializer.getDomConfig().setParameter("xml-declaration", false);
String result = serializer.writeToString(txns);

log.info(result); //I sse here same result both in Windows as in Unix

Document d2 = convertStringToDocument(result);
q.addMessages( d2.getDocumentElement());

第二步:有一个非常复杂的流程更改和添加新字段。最后使用此方法保存在某个临时文件中:

synchronized protected void writeToFile(Node node, String file)
throws SAXException, IOException {

try {

StringWriter output = new StringWriter();

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(output));

String xml = output.toString();

Integer whereIs = xml.indexOf("<?xml");

/*both in Windows as in Unix I will find <?xml in position 0, so no extra character before <?xml */
if (whereIs >= 0) {
    log.info("<?xml is in " + whereIs + " position");
}

FileWriter filewriter = new FileWriter(file);

/* The replace below was a clue found in some forum for taking the BOM out in case it exists */
filewriter.write(((xml.replace("\uFEFF", "")).replace("\uFEFF", "")).replace("\uFFFE", ""));
filewriter.close();

} catch (Exception ex) {
    System.out.println(ex.getMessage());
}
}

第三步:在解析临时文件时出现错误。请参阅下面我尝试过的两种方法,它们都可以在 WIndows 中运行,但不能在 Unix 中运行

//我读过几个论坛指出 BOM 问题之前的版本

       public Node readFromFile(String file) throws ParserConfigurationException {

DocumentBuilderFactory docFactory = DocumentBuilderFactory
                           .newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document d = null;
try {

d = docBuilder.parse(file);

} catch (Exception e) {
  System.out.println(e.getMessage());
}
return d.getDocumentElement();
}

//在与BOM问题相关的论坛中找到一些线索后的版本 公共(public)节点 readFromFile(字符串文件) {

try {
java.io.File f = new java.io.File(file);
java.io.InputStream inputStream = new java.io.FileInputStream(f);

// Checking if there is BOM
BOMInputStream bomIn = new BOMInputStream(inputStream,ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE);

//it always show that there is no BOM in both Windows as Unix
if (bomIn.hasBOM() == false) {
    log.info("No BOM found");
}

java.io.Reader reader = new java.io.InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document d = null;

log.info("Before parsing file"); //this is the last log while in Unix before the below error

/*Next line will cause issue only in Unix
ÝFatal Error¨ myFile.xml:1:39: Content is not allowed in prolog.
Content is not allowed in prolog.*/

d = docBuilder.parse(is);

log.info("After parsing file"); //this will be showed while in Windows

return d.getDocumentElement();
} catch (Exception e) {
    log.info(e.getMessage());
    return null;
}
}

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <groupId>com.mycomp.batchs</groupId>
       <artifactId>AuthorizationFileToICTTQueue</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>

       <name>AuthorizationFileToICTTQueue</name>
       <url>http://maven.apache.org</url>

       <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <spring.framework.version>4.2.4.RELEASE</spring.framework.version>
              <spring.batch.version>3.0.6.RELEASE</spring.batch.version>
              <log4j.version>1.2.7</log4j.version>
              <java.version>1.7</java.version>
              <maven.compiler.plugin.version>2.1</maven.compiler.plugin.version>
              <hsqldb.version>1.8.0.10</hsqldb.version>
              <logback-classic.version>1.1.5</logback-classic.version>
       </properties>

       <dependencies>
              <dependency>
                     <groupId>commons-io</groupId>
                     <artifactId>commons-io</artifactId>
                     <version>2.4</version>
              </dependency>


              <dependency>
                     <groupId>org.springframework.batch</groupId>
                     <artifactId>spring-batch-core</artifactId>
                     <version>${spring.batch.version}</version>
              </dependency>
              <dependency>
                     <groupId>org.springframework.batch</groupId>
                     <artifactId>spring-batch-infrastructure</artifactId>
                     <version>${spring.batch.version}</version>
              </dependency>
              <dependency>
                     <groupId>ch.qos.logback</groupId>
                     <artifactId>logback-classic</artifactId>
                     <version>${logback-classic.version}</version>
              </dependency>

              <dependency>
                     <groupId>org.springframework</groupId>
                     <artifactId>spring-tx</artifactId>
                     <version>${spring.framework.version}</version>
              </dependency>

              <dependency>
                     <groupId>org.springframework</groupId>
                     <artifactId>spring-jdbc</artifactId>
                     <version>${spring.framework.version}</version>
              </dependency>
              <dependency>
                     <groupId>hsqldb</groupId>
                     <artifactId>hsqldb</artifactId>
                     <version>${hsqldb.version}</version>
              </dependency>

       </dependencies>

       <build>
              <plugins>
                     <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-compiler-plugin</artifactId>
                           <version>${maven.compiler.plugin.version}</version>
                           <configuration>
                                  <source>${java.version}</source>
                                  <target>${java.version}</target>
                           </configuration>
                     </plugin>

              </plugins>
       </build>
</project>

**** 编辑于 2016 年 2 月 18 日 01:00Pm 巴西利亚时区 使用 OpenText 连接 - Connection Central for x64 从 zOS/390 传输 第一个图像显示以 ASCII 传输的文件。第二张图显示了以二进制形式传输的文件

Transfer Mode = ASCII

Transfer Mode = Binary

最佳答案

听起来像是字符集问题,XML 序言可能是

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

如果您安装的 *nix 无论出于何种原因不支持 UTF,则该文件的格式将无法正确。难道是当您创建/复制文档到 *nix 时字符集被搞砸了并且不是您期望的 UTF-8 吗?在两个平台上使用十六进制编辑器检查文件可能是有意义的。

我知道我以前遇到过这个问题,尽管通常是其他方式,但我没有当前的示例,它不起作用,只知道这是一个字符集问题。

关于java - 序言中不允许出现内容 - Unix 中发生异常,但使用相同代码的 Windows 中则不会发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36657425/

相关文章:

java - Android:从服务将字节转换为位图数据为空

java - 放置自动生成代码的好地方?

javascript - 解析无效的 JSON

c - 根据初始格式解析字符串

java - 使用 forEach() 时如何调用实例方法

java - 是否可以使用支持传输的 netty 和 arterr 来运行 akka 系统?

java - 寻找 AspectJ 解析器

c - 相当于 c 中的 rm –rf

linux - 如何在 linux 中查找 jar 没有给定的字符串

bash - Unix 中的 "./"运算符是什么?