java 如何调试在写入字符串时导致转换错误的XML对象?

oalqel3c  于 2023-01-29  发布在  Java
关注(0)|答案(4)|浏览(116)

我有一个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确实说明了包含的异常是空指针异常。它适用于同一程序中的其他相关对象,但我不知道如何调试。
EDIT以下是堆栈跟踪:

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)
olqngx59

olqngx591#

我猜错误出在您的某个对象上,要么是因为该对象不是有效的XML,要么是因为XML具有空(null)文本节点而不是空字符串值。
参见java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer. ToUnknownStream.characters(ToUnknownStream.java:338)
根据@Sajan Chandran评论中的链接:
http://dotcommers.wordpress.com/2008/10/22/javaxxmltransformtransformerexception-javalangnullpointerexception-how-to-solve/
如果你看一下,你会看到代码。

public void characters(String chars) throws SAXException
{
  final int length = chars.length();

char.length()的问题为零,因为文本节点为空。
正如它所指出的,要解决这个问题,只需确保所有XML节点都具有字符串值,而不是“null”值。
如果这个方法不起作用,你可以发布一个导致异常抛出的XML对象的例子吗?另外,你可以检查该对象是否是有效的XML,以及字符实体等是否被正确编码吗?

c86crjj0

c86crjj02#

基本同意其他答案,除了:
问题在于,在doce数据结构中,有一个文本节点包含 null空)字符内容。
虽然Kiran的帖子(由Sajan链接)很有帮助,但我很确定他弄错了“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
  • 检查每个文本节点是否有空字符数据

例如(仍为半伪码)

if (n.getType() == org.w3c.dom.Node.TEXT_NODE && n.getNodeValue() == null) {
     throw new Exception("Text node with null content: " +
         path to this node);
 }

这将帮助您找到任何包含空字符内容的文本节点的位置,这将帮助您找出它们的来源。无论如何,这些doce结构是在哪里创建的?

sgtfey8w

sgtfey8w3#

我同意另一个答案,下面是我用来删除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));
            }
        }
    }
mfpqipee

mfpqipee4#

是的,我在翻译器中得到了NPE。早些时候我使用的是SAXON解析器,工作正常,但是当我切换到com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl时,开始失败。所以在transformer.transform()之前,我调用了这个API deleteNullNode(节点节点)...它工作起来很有魅力...

相关问题