Transformer的变换导致致命错误,为什么?
我已经使用 JAXP 构建了一个文档,如下所示:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement = document.createElement("Root");
for (MyObject o : myCollection) {
Element entry = document.createElement("Entry");
Element entryItem = document.createElement("EntryItem");
entryItem.appendChild(document.createTextNode(o.getProperty()));
entry.appendChild(entryItem);
rootElement.appendChild(entry);
}
document.appendChild(rootElement);
现在,当我尝试像这样输出文档的 XML 时:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
System.out.println(result.getWriter().toString());
它在 transformer.transform
行上崩溃,并出现以下错误:
FATAL ERROR: 'java.lang.NullPointerException'
:null
我该如何做去调试这个吗?我已确保 transformer
、source
和 result
不为空。
I've built a document using JAXP like this:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement = document.createElement("Root");
for (MyObject o : myCollection) {
Element entry = document.createElement("Entry");
Element entryItem = document.createElement("EntryItem");
entryItem.appendChild(document.createTextNode(o.getProperty()));
entry.appendChild(entryItem);
rootElement.appendChild(entry);
}
document.appendChild(rootElement);
Now, when I try to output the XML for the document like this:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
System.out.println(result.getWriter().toString());
It falls apart on the transformer.transform
line with the following error:
FATAL ERROR: 'java.lang.NullPointerException'
:null
How do I go about debugging this? I've made sure that transformer
, source
and result
aren't null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测这是:
创建了一个具有空值的文本节点。查看 Xerces 的代码(这是 Oracle JDK 1.6 附带的默认 JAXP 实现),我发现在构造文本节点时没有进行空验证。我怀疑这会导致 Transformer 死掉。
要么是这样,要么是存在一些 JAXp 配置问题。
您可能希望设置
jaxp.debug
系统属性(JDK 1.6+可用)来获取一些JAXP跟踪信息。I'm guessing that this:
created a text node with a null value. Looking at Xerces' code (which is the default JAXP implementation shipped with Oracle's JDK 1.6), I see no null validation done at the time of constructing the text node. I suspect that that, later, makes the
Transformer
die.Either that, or there's some JAXp configuration problem.
You may wish to set the
jaxp.debug
system property (available JDK 1.6+) to get some JAXP tracing information.——文件怎么样?
哎呀,抱歉,显然第二部分在第一部分之后:)您使用的是哪个解析器?
--How about the document?
Ooops sorry, obviously the second part follows the first :) Which parser are you using?