【发布时间】:2016-08-08 00:47:55
【问题描述】:
我已经在这个问题上苦苦挣扎了一个多星期。我可能阅读了超过 50 篇关于它的不同页面,但在我的案例中找不到解决方案。
如果没有一个具体点,我的问题肯定会出现重复:我的代码确实在 Windows 中工作,并且相同的代码在 Unix 中运行时会导致该主题的问题。
基本上,在论坛中进行的所有搜索都让我明白这是 BOM 的问题。我遵循了所有建议,我的代码在 Windows 中继续运行,但在 Unix Mainframe 中导致了同样的问题。
在下面找到我的代码中最相关的步骤以及我尝试过的注释尝试。很难想象还有什么可以做的,因为从一开始我的代码就在 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问题相关线索后的版本 公共节点 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 日下午 1:00 巴西利亚时区 使用 OpenText Connectivity 从 zOS/390 传输 - Connection Central for x64 第一张图片显示了以 ASCII 格式传输的文件。第二张图片显示了作为二进制传输的文件
【问题讨论】:
-
您可以在十六进制编辑器/查看器中检查生成的文件吗?文件的前 100 个字节是多少(十六进制)?
-
Alohci,我从临时文件中添加了前 100 个字节。配对时发生错误。我只知道一种检查方法。我使用 OpenText FTP 功能将文件从 Mainframe 下载到本地 Windows,然后从 mh-nexus 在 Hexeditor 中打开它。你知道我是否应该直接在 Unix 中打开文件来显示前 100 字节吗?我想知道在将文件从大型机移动到我的 PC 时是否更改了文件格式。我究竟需要查看前 100 个字节,才能知道如何修复它?您可能会在我的代码中看到我包含的代码以保证我没有 BOM 字符
-
自从我使用 Unix 以来已经有很长一段时间了(大约 30 年),但我认为有一个命令“od -x filename”会给你一个文件的十六进制转储。我也不知道要查找什么,但我在您发布的 xml 序言中没有看到任何可疑内容,抱歉。