gpt4 book ai didi

java - 如何将数组字节转换为 org.w3c.dom.Document

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:53 26 4
gpt4 key购买 nike

我有一个文档 (org.w3c.dom.Document),我将这个文档转换为字节数组:

private byte[] obtenerBytesDeDocument(Document documentoXml) throws Exception {
Source source = new DOMSource( documentoXml );
ByteArrayOutputStream out = new ByteArrayOutputStream();
Result result = new StreamResult(out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
byte[] butesXml = out.toByteArray();
return butesXml;
}

我需要将字节数组转换为新文档:

private Document obtenerDocumentDeByte(byte[] documentoXml) throws Exception {
...
}

有什么想法吗?

谢谢!!!

最佳答案

以下应该有效。

private Document obtenerDocumentDeByte(byte[] documentoXml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(documentoXml));
}

关于java - 如何将数组字节转换为 org.w3c.dom.Document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21165871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com