prolog中不允许内容 - 异常发生在Unix中,但在具有相同代码的Windows中不发生

时间:2016-04-15 21:51:45

标签: java parsing unix dom sax

我在这个问题上已经挣扎了一个多星期。我可能阅读了50多个关于它的不同页面,但在我的案例中找不到解决方案。

当然,如果没有一个特定的要点,我的问题会显得重复:我的代码在Windows中运行,同样的代码,在Unix中运行导致此主题的问题。

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

在下面找到我的代码中最相关的步骤,并评论我尝试过的试用版。自从开始我的代码在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 node readFromFile(String file){

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 Brasilia Timezone 使用OpenText连接从zOS / 390转移 - 用于x64的Connection Central First Image显示传输为ASCII的文件。第二个图像显示文件转换为二进制

Transfer Mode = ASCII

Transfer Mode = Binary

1 个答案:

答案 0 :(得分:2)

听起来像是字符集问题,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?在两个平台上都可以使用十六进制编辑器检查文件。

我知道我之前遇到过这种情况,虽然通常是另一种方式,但我没有当前的例子,它不起作用,只知道这是一个字符集问题。