我正在使用 StaX XMLEventReader 和 XMLEventWriter。 我需要对保存在字节数组中的原始 xml 文件进行修改后的临时副本。如果我这样做(为了调试,正在写入文件):
public boolean isCrcCorrect(Path path) throws IOException, XPathExpressionException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEventReader reader = null;
XMLEventWriter writer = null;
StreamResult result;
String tagContent;
if (!fileData.currentFilePath.equals(path.toString())) {
parseFile(path);
}
try {
System.out.println(path.toString());
reader = XMLInputFactory.newInstance().createXMLEventReader(new FileReader(path.toString()));
//writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
writer = XMLOutputFactory.newInstance().createXMLEventWriter(new FileWriter("f:\\Projects\\iqpdct\\iqpdct-domain\\src\\main\\java\\de\\iq2dev\\domain\\util\\debug.xml"));
writer.add(reader);
writer.close();
} catch(XMLStreamException strEx) {
System.out.println(strEx.getMessage());
}
crc.reset();
crc.update(output.toByteArray());
System.out.println(crc.getValue());
//return fileData.file_crc == crc.getValue();
return false;
}
克隆与起源不同 来源:
<VendorText textId="T_VendorText" />
克隆:
<VendorText textId="T_VendorText"></VendorText>
为什么他要加上结束标签? Source 中也没有。
最佳答案
如果您想要恰好是 XML 文档的字节流的精确副本,则必须将其复制为字节流。您无法通过向 XML 解析器提供后端来复制它,因为解析器前端的目的是将您的代码与可能有所不同但在语义上等效的功能隔离开来。例如,在您的情况下,有两种指示空元素的方法。
关于java - 如何克隆 xml 文件(完全相同的副本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26687909/