简体   繁体   English

无法将dom文档写入xml文件

[英]can't write dom document to xml file

I have spring mvc project in whicn I upload xml file and try to write it to the my xml by DOM, but confront with this exception. 我在上载xml文件并尝试通过DOM将其写入我的xml的spring mvc项目中,但是遇到了这个异常。 When I make attempts to do the same proccesses in usual project - all works fine. 当我尝试在常规项目中执行相同的过程时,一切正常。 Can't understand why it happens.. 无法理解为什么会发生..

    type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)


root cause 

java.lang.NullPointerException
    org.apache.xml.utils.TreeWalker.dispatachChars(TreeWalker.java:303)
    org.apache.xml.utils.TreeWalker.startNode(TreeWalker.java:447)
    org.apache.xml.utils.TreeWalker.traverse(TreeWalker.java:202)
    org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:343)
    com.mycompany.xmlloader.xmlHandler.DOMParser.saveToCatalog(DOMParser.java:147)
    com.mycompany.xmlloader.service.XMLServiceImpl.updateCatalog(XMLServiceImpl.java:48)
    com.mycompany.xmlloader.controller.XMLController.uploadToCatalog(XMLController.java:47)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

And snippet of code: 和代码段:

TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        StreamResult result = new StreamResult(catalogFile.toURI().getPath());
        Source source = new DOMSource(doc);
        trans.transform(source, result);

The likely cause of your problem is a Text node in your Document which has its data set to null . 问题的可能原因是Document的“ Text节点,其数据设置为null (See here and here for discussions of the problem.) (有关问题的讨论,请参见此处此处 。)

You can check your Document to see if this diagnosis is correct using the following code: 您可以使用以下代码检查Document以查看此诊断是否正确:

public static void checkDoc(Node n) {
  if (n instanceof Text) {
    if (((Text) n).getData() == null) {
      System.err.println("null data!!!!");
    }
  }

  NodeList l = n.getChildNodes();
  for (int i = 0; i < l.getLength(); ++i) {
    checkDoc(l.item(i));
  }
}

Call checkDoc on your Document : 在您的Document上调用checkDoc

checkDoc(doc);

If you see any output, then you now know the immediate cause for your NPE. 如果看到任何输出,则现在知道NPE的直接原因。 The solution to your problem is then: Do not build a Document containing Text nodes with null data. 然后,解决问题的方法是:不要构建包含具有空数据的Text节点的Document

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM