无法将dom文件写入xml文件

时间:2012-10-02 12:53:04

标签: java xml xml-parsing

我有一个spring mvc项目,我上传了xml文件并尝试通过DOM将它写入我的xml,但是遇到了这个异常。当我试图在通常的项目中做同样的过程 - 一切正常。无法理解为什么会发生..

    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)

代码片段:

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

1 个答案:

答案 0 :(得分:4)

问题的可能原因是Text中的Document节点的数据设置为null。 (有关问题的讨论,请参阅herehere。)

您可以使用以下代码检查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));
  }
}

checkDoc上致电Document

checkDoc(doc);

如果您看到任何输出,那么您现在知道NPE的直接原因。您的问题的解决方案是:不要构建包含DocumentText节点的{{1}}。