0

我正在尝试使用 输出我的 XML 文档Transformer.transform(Source, Result),但它不会导出文档,只会抛出FileNotFoundException. 我正在尝试/WebContent/samples/users.xml使用 Apache Tomcat 将其导出到我的项目中。在 Google 上做了一些工作后,我在bugs.sun.com上的所谓“错误”上找到了这个页面。

这是我的代码的选择:

Document doc = newDocument();
Element rootElement = doc.createElement("homeworkManager");

Element sampleUserElement = doc.createElement("user");
// (set username and password)

Element sampleHomeworkElement1 = doc.createElement("homework");
// (set homework attributes)

sampleUserElement.appendChild(sampleHomeworkElement1);
rootElement.appendChild(sampleUserElement);
doc.appendChild(rootElement);

// HomeworkManager.getRealPath(String) is the same as  HttpSession.getServletContext().getRealPath(String)
File output = new File(HomeworkManager.instance().getRealPath("/") + "/samples/users.xml");


Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

transformer.transform(new DOMSource(doc), new StreamResult(output));  // EXCEPTION THROWN HERE



这是堆栈跟踪:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: ...\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Homework Manager\samples\users.xml (The system cannot find the path specified)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:493)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:302)
at net.dean.homeworkmanager.UserManager.createSampleXMLFile(UserManager.java:215)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:91)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

Caused by: java.io.FileNotFoundException: (...)\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Homework Manager\samples\users.xml (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(TransformerImpl.java:466)
... 25 more

如您所知,我很困惑。任何建议将不胜感激。

编辑:这只发生在 Web 项目上。我可以使用完全相同的代码在桌面应用程序上导出我的文档。

编辑 2:即使我创建了目录 users.xml,文件也不会被修改。相反,它是在(...)\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Homework Manager\samples\users.xml

4

0 回答 0