gpt4 book ai didi

java - 文档似乎为空,但我可以获取第一个元素

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:44 25 4
gpt4 key购买 nike

我用以下代码构建了一个 DOM 文档:

private org.w3c.dom.Document createXml(WorkObject workObject, String hierarchy, String dateFormat) {
XMLBuilder2 builder = XMLBuilder2.create(hierarchy);
Map<String, Object> localVariables = workObject.getLocalVariablesMap();
builder = createXmlFromMap(localVariables, builder, dateFormat);
Properties outputProperties = new Properties();
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "no");
String xmlString = builder.asString(outputProperties);
LOG.debug("XML string: " + xmlString);
org.w3c.dom.Document xmlDoc = builder.getDocument();
LOG.debug("DOM root element: " + xmlDoc.getDocumentElement().getTagName());
return xmlDoc;
}

builder.asString()调用返回预期的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<myVar>a value</myVar>
<docId>GEAR-b3384419-36f8-48fc-b194-c937e7afc0dd</docId>
</root>

最后一个 LOG 语句的输出如预期的那样是 root

但是当我使用这段代码时,xmlDoc 似乎有些奇怪......

xmlDoc 传递给此构造函数时:

private org.w3c.dom.Document mXmlDoc;

public XmlMailMergeDataSet(org.w3c.dom.Document xmlDoc) {
mXmlDoc = xmlDoc;
}

在IDEA中调试时,xmlDocmXmlDoc的值为[#document: null]

不幸的是,我无法进一步调试以查看随后发生的情况,因为 XmlMailMergeDataSet 传递到的代码不是开源的。

我在这里遗漏了一些明显的东西吗?

最佳答案

[#document: null] 并不意味着 Document 引用为 null,也不意味着树为空。它只是计算 xmlDoc.toString()) 的结果的字符串表示形式。我猜这里的 null 是文档节点的“节点值”,在这种情况下没有任何意义(DocumentNode 的特殊子类型)。

因此,您的代码中的 Document 实例似乎没有任何问题。

关于java - 文档似乎为空,但我可以获取第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27721107/

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