0

我正在尝试从 java 代码编辑 xml 文件。这是方法:

public static void changeReportPath(String reportPath){

      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder;
      try {
    docBuilder = docFactory.newDocumentBuilder();
    File file = new File(reportPath);
    Document doc = docBuilder.parse(reportPath);

    // Get the root element
    Node root = doc.getFirstChild();
    // Get the staff element by tag name directly
    NodeList parameter = doc.getElementsByTagName("defaultValueExpression");

    NodeList defaultValueExpression =parameter.item(0).getChildNodes();

        defaultValueExpression.item(0).setTextContent('"'+"/home/amira/work/"+'"');

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    StreamResult result = new StreamResult(new File(reportPath));


    transformer.transform(source, result);


      } catch (ParserConfigurationException e) {

    e.printStackTrace();
      } catch (SAXException e) {

    e.printStackTrace();
      } catch (IOException e) {

    e.printStackTrace();
      } catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      } catch (TransformerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      }


}

这是方法调用:

 Utilities.changeReportPath("/home/amira/xx/runtime/uploads/report/project/myfile.jrxml");

这是错误日志:


java.io.FileNotFoundException: file:/home/amira/xx/runtime/uploads/report/project/DEVFACCOA.jrxml (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
    at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:235)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:278)
    at com.kopimap.utilities.Utilities.changeReportPath(Utilities.java:205)
    at com.example.kopimapreportgeneration.KopimapreportgenerationUI$ReportUploader.uploadSucceeded(KopimapreportgenerationUI.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:968)
    at com.vaadin.ui.Upload.fireUploadSuccess(Upload.java:811)
    at com.vaadin.ui.Upload$1.streamingFinished(Upload.java:1070)
    at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:554)
    at com.vaadin.server.communication.FileUploadHandler.handleFileUploadValidationAndData(FileUploadHandler.java:419)
    at com.vaadin.server.communication.FileUploadHandler.doHandleSimpleMultipartFileUpload(FileUploadHandler.java:382)
    at com.vaadin.server.communication.FileUploadHandler.handleRequest(FileUploadHandler.java:267)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1329)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:236)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)

我在这里找到了这个输入链接描述

所以我改变了这一行:

    Document doc = docBuilder.parse(file.toURI().toString());

但问题依然存在

4

2 回答 2

1

使用文件而不是 URI:

Document doc = docBuilder.parse(reportPath);

将其替换为:

Document doc = docBuilder.parse(file);

根据Java Doc:

public class FileNotFoundException extends IOException

表示尝试打开由指定路径名表示的文件失败的信号。

当具有指定路径名的文件不存在时,FileInputStream、FileOutputStream 和 RandomAccessFile 构造函数将引发此异常。如果文件确实存在但由于某种原因不可访问,例如当试图打开只读文件进行写入时,这些构造函数也会抛出它。

所以你要么没有文件,要么没有所需的权限

于 2013-07-01T11:59:03.013 回答
1

我不确定,但认为你的问题是定义路径。我的意思是你应该例如将你的项目根路径添加到你的文件地址。

于 2013-07-01T12:02:48.313 回答