序言中不允许内容 - 异常发生在 Unix 中,但在 Windows 中没有,代码相同
Content is not allowed in prolog - exception happens in Unix but not in Windows with same code
我在这个问题上纠结了一个多星期。我可能阅读了 50 多个不同的页面,但我找不到适合我的解决方案。
当然,如果没有一个特别的问题,我的问题会显得重复:我的代码在 Windows 中确实有效,并且相同的代码,而在 Unix 中的 运行 导致了这个主题的问题.
基本上,在论坛中进行的所有搜索都让我明白这是 BOM 的问题。我听从了所有建议,我的代码在 Windows 中继续工作,但它在 Unix 大型机中导致了同样的问题。
在下面找到我的代码中最相关的步骤和我尝试过的评论试验。很难想象还有什么可以做的,因为从一开始我的代码就是 Windows 中的 运行 但只在 Unix Mainframe
中导致了 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>
**** 编辑于 18/Feb/2016 01:00Pm 巴西利亚时区
使用 OpenText 连接从 zOS/390 传输 - x64 的连接中心
第一张图片显示以 ASCII 格式传输的文件。第二张图片显示以二进制形式传输的文件
听起来像是字符集问题,XML prolog 可能是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
并且如果您的 *nix 安装出于某种原因不支持 UTF,那么文件将不会被正确格式化。可能是当您 created/copied 将文档提交给 *nix 时字符集搞砸了并且不是您期望的 UTF-8 吗?在两个平台上使用十六进制编辑器检查文件可能有意义。
我知道我之前已经 运行 了解过这个问题,虽然通常是相反的,但我没有当前的例子表明它不起作用,只知道这是一个字符集问题。
我在这个问题上纠结了一个多星期。我可能阅读了 50 多个不同的页面,但我找不到适合我的解决方案。
当然,如果没有一个特别的问题,我的问题会显得重复:我的代码在 Windows 中确实有效,并且相同的代码,而在 Unix 中的 运行 导致了这个主题的问题.
基本上,在论坛中进行的所有搜索都让我明白这是 BOM 的问题。我听从了所有建议,我的代码在 Windows 中继续工作,但它在 Unix 大型机中导致了同样的问题。
在下面找到我的代码中最相关的步骤和我尝试过的评论试验。很难想象还有什么可以做的,因为从一开始我的代码就是 Windows 中的 运行 但只在 Unix Mainframe
中导致了 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>
**** 编辑于 18/Feb/2016 01:00Pm 巴西利亚时区 使用 OpenText 连接从 zOS/390 传输 - x64 的连接中心 第一张图片显示以 ASCII 格式传输的文件。第二张图片显示以二进制形式传输的文件
听起来像是字符集问题,XML prolog 可能是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
并且如果您的 *nix 安装出于某种原因不支持 UTF,那么文件将不会被正确格式化。可能是当您 created/copied 将文档提交给 *nix 时字符集搞砸了并且不是您期望的 UTF-8 吗?在两个平台上使用十六进制编辑器检查文件可能有意义。
我知道我之前已经 运行 了解过这个问题,虽然通常是相反的,但我没有当前的例子表明它不起作用,只知道这是一个字符集问题。