Java 将字节保存到xmlfile。无效的Unicode字符

Java 将字节保存到xmlfile。无效的Unicode字符,java,xml,hash,Java,Xml,Hash,标题差不多就是这么说的 我计算了一个文本文件的SHA1哈希,到目前为止还不错。我只使用普通的FileOutputStream将其写入.xml文件中 FileOutputStream fos = new FileOutputStream(out.xml); fos.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>".getBytes()); fos.write("<Digest xmlns=\

标题差不多就是这么说的

我计算了一个文本文件的SHA1哈希,到目前为止还不错。我只使用普通的FileOutputStream将其写入.xml文件中

FileOutputStream fos = new FileOutputStream(out.xml);
fos.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>".getBytes());
fos.write("<Digest xmlns=\"http://testnamespace\"".getBytes());
fos.write(getFileHash("C:\\myfile.txt"));
fos.write("</Digest>".getBytes()); 
fos.flush(); 
fos.close();
FileOutputStream fos=新的FileOutputStream(out.xml);
fos.write(“.getBytes());

fos.write(“我假设,
getFileHash
方法返回二进制数据(如字节[])。但是,XML是文本,因此不能将任意二进制数据放入其中。解决此问题的一种方法是将二进制数据转换为字符,例如使用十六进制编码或base64编码


此外,各种
getBytes()
调用将根据平台编码返回字节,该编码可能是UTF-8,也可能不是UTF-8。明确指定编码更安全。

我强烈建议您使用XML库来编写XML。我怀疑您未能引用某些内容。该库将阻止您编写无效的XML。FWIW我使用XOM库