我有一个XML对象,当我用
将其转换为字符串时public static String XMLElementToString(Document doc, Element e) {
// --- Output XML ---
try {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
StringWriter buffer = new StringWriter();
Result result = new StreamResult(buffer);
Source source = null;
if (e != null) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
source = new DOMSource(e);
} else {
source = new DOMSource(doc);
}
transformer.transform(source, result); // <-- Error occurs here
buffer.flush();
return buffer.toString();
} catch (TransformerException ex) {
System.out.println("exception: " + ex.getMessage());
}
return "";
}
我的一个对象出错了。使用断点并查看xml对象我不能说为什么,尽管TransformerException确实说包含的异常是一个空指针异常。它适用于我在同一个程序中的其他相关对象,但我不知道如何调试它。
[[编辑]] 这是堆栈跟踪:
ERROR: ''
javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:716)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
at com.protocase.utils.StringHelpers.XMLElementToString(StringHelpers.java:132)
at com.protocase.io.PDWriter.writePD(PDWriter.java:49)
at com.protocase.io.PDWriter.writePDA(PDWriter.java:26)
at com.protocase.viewer.JDesigner.OnSaveAs(JDesigner.java:1364)
at com.protocase.viewer.JDesigner.access$000(JDesigner.java:106)
at com.protocase.viewer.JDesigner$34.actionPerformed(JDesigner.java:722)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at com.protocase.viewer.EventQueueProxy.dispatchEvent(JDesigner.java:2338)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(ToUnknownStream.java:338)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:240)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:132)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:661)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707)
... 45 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(ToUnknownStream.java:338)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:240)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:132)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:661)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
at com.protocase.utils.StringHelpers.XMLElementToString(StringHelpers.java:132)
at com.protocase.io.PDWriter.writePD(PDWriter.java:49)
at com.protocase.io.PDWriter.writePDA(PDWriter.java:26)
at com.protocase.viewer.JDesigner.OnSaveAs(JDesigner.java:1364)
at com.protocase.viewer.JDesigner.access$000(JDesigner.java:106)
at com.protocase.viewer.JDesigner$34.actionPerformed(JDesigner.java:722)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at com.protocase.viewer.EventQueueProxy.dispatchEvent(JDesigner.java:2338)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
答案 0 :(得分:15)
我猜这个错误是你的一个对象,因为该对象不是有效的XML,或者因为XML有一个空(null)文本节点而不是空字符串值。
请参阅java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer. ToUnknownStream.characters(ToUnknownStream.java:338)
基于@Sajan Chandran评论中的链接:
如果你看一下,你会看到代码。
public void characters(String chars) throws SAXException
{
final int length = chars.length();
由于空文本节点,char.length()
的问题为零。
正如其所述,要解决此问题,只需确保所有XML节点都具有字符串值而不是“null”值。
如果这不起作用,您可以发布导致引发异常的XML对象的示例。此外,您是否可以检查对象是否是有效的XML,以及字符实体等是否已正确编码。
答案 1 :(得分:4)
基本上同意其他答案,除了:
问题是您的doc
或e
数据中包含 null (不为空)字符内容的文本节点结构
虽然Kiran的帖子(由Sajan链接)很有用,但我很确定他错误地说“chars.length()为零,因为尝试了一个空文本节点插入。” chars.length()
不是零;对chars.length()
的调用引发了NPE,因为chars
为空。
要找到问题,请在此处添加方法调用:
if (e != null) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
source = new DOMSource(e);
checkForNullTextNodes(e); // ADDED
} else {
source = new DOMSource(doc);
checkForNullTextNodes(doc); // ADDED
}
transformer.transform(source, result); // <-- Error occurs here
然后将checkForNullTextNodes(node)
定义为:(伪代码)
n
的树E.g。 (仍然是半伪代码)
if (n.getType() == org.w3c.dom.Node.TEXT_NODE && n.getNodeValue() == null) {
throw new Exception("Text node with null content: " +
path to this node);
}
这应该可以帮助您找出具有空字符内容的任何文本节点的位置,这可以帮助您找出它们的来源。无论如何,这些doc
或e
结构在哪里被创建?
答案 2 :(得分:4)
我同意另一个答案,这是我用来摆脱null
节点的代码
public void deleteNullNode(Node racine)
{
NodeList nl = racine.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
if (nl.item(i).getNodeType() == Node.TEXT_NODE && nl.item(i).getNodeValue() == null)
{
nl.item(i).getParentNode().removeChild(nl.item(i));
}
else
{
deleteNullNode(nl.item(i));
}
}
}