提问人:Leo Filip Kovačić 提问时间:3/31/2023 最后编辑:Leo Filip Kovačić 更新时间:3/31/2023 访问量:192
尝试将字节数组解析为 xml 文档时获取 null 值
Getting a null value while trying to parse a byte array to an xml Document
问:
我正在从数据库中存储为字节数组的 xml 文档中提取值。我正在尝试在解压缩后传递该字节数组的值。我没有太多使用xmls的经验,所以完全有可能我做错了。这是我到目前为止的代码:
public static Document getXmlFromByteArray(byte[] array) throws IOException {
try {
String xmlString = new String(array, StandardCharsets.UTF_8);
LOGGER.info(xmlString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlString));
Document document = builder.parse(is);
LOGGER.info(document.toString());
return document;
} catch (Exception e) {
throw new IOException(e);
}
}
在这里,我使用javax.xml.parsers.DocumentBuilder作为解析器,并使用org.w3c.dom.Document作为文档
现在,我已经确保了字节数组和字符串都设置正确,这是我从中得到的日志示例
INFO: [类] INFO: [类] [#document: null]<?xml version="1.0" encoding="utf-8"?><Signatures>dummy signature</Signatures>
如果有人能为我指出正确的方向,我将不胜感激。如果我不知道如何使用
我最初尝试这样做:
public static Document getXmlFromByteArray(byte[] array) throws IOException {
try {
LOGGER.info(Arrays.toString(array));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(array);
InputSource input = new InputSource(bis);
Document document = builder.parse(input);
LOGGER.info(document.toString());
if (document == null)
throw new IOException("Error converting to xml!");
return document;
} catch (Exception e) {
throw new IOException(e);
}
}
此后尝试手动将字节数组转换为字符串并添加 字符串 xmlString = new String(array, StandardCharsets.UTF_8);行来确保文档本身的内容有效,以及从数据库中手动获取、解压缩和读取 XML。
答:
0赞
Leo Filip Kovačić
3/31/2023
#1
似乎document.toString()没有达到我的预期。尽管记录的值是“[#document: null]”,但此处实际正在读取文档。
我像这样修改了我的代码:
public static Document getXmlFromByteArray(byte[] array) throws IOException {
try {
String xmlString = new String(array, StandardCharsets.UTF_8);
LOGGER.info(xmlString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlString));
Document document = builder.parse(is);
LOGGER.info(document.getXmlVersion());
NodeList nodeList = document.getElementsByTagName("Signatures");
if (nodeList.getLength() > 0) {
Element signaturesElement = (Element) nodeList.item(0);
String signaturesText = signaturesElement.getTextContent();
LOGGER.info("Signatures Text: " + signaturesText);
}
return document;
} catch (Exception e) {
throw new IOException(e);
}
}
我现在按预期获得“1.0”和“签名文本:虚拟签名”。
评论
document.toString()
document
document
NullPointerException
LOGGER.info(document.getXmlVersion()); NodeList nodeList = document.getElementsByTagName("Signatures"); if (nodeList.getLength() > 0) { Element signaturesElement = (Element) nodeList.item(0); String signaturesText = signaturesElement.getTextContent(); LOGGER.info("Signatures Text: " + signaturesText); }