转换时找不到正确位置的XML文件

时间:2019-02-01 09:15:39

标签: java

我试图使用DOM修改xml文件,并且发生了这种情况: 我的代码是:

public class XsltFolderWise {
    public static void main(String[] args) {
        File curDir = new File(
                "C:/Users/rimancha/Desktop/SaveDir/11439"); // current
                                                                    // directory
        displayDirectoryContents(curDir);
    }

    public static void displayDirectoryContents(File dir) {
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("directory:" + file.getCanonicalPath());
                    displayDirectoryContents(file);

                } else {
                    System.out.println("file:" + file.getCanonicalPath());
                    String XSLFILE = "C:/Users/rimancha/Desktop/SaveDir/jars/command-reference.xsl";
                    File INFILE = file;
                    String OUTFILE = "C:/Users/rimancha/Desktop/SaveDir/output_xslt/output"
                            + file;
                    StreamSource xslCode = new StreamSource(new File(XSLFILE));
                    StreamSource input = new StreamSource((INFILE));
                    File out=new File(OUTFILE);
                    if (!out.exists()) {
                        System.out.println(OUTFILE
                                + " created successfully? "
                                + out.mkdirs());
                    }
                    StreamResult output = new StreamResult(out);

                    TransformerFactory tf = TransformerFactory.newInstance();

                    Transformer trans = tf.newTransformer(xslCode);
                    trans.setOutputProperty(OutputKeys.INDENT, "yes");
                    trans.setOutputProperty(
                            "{http://xml.apache.org/xslt}indent-amount", "4");

                    try {
                        trans.transform(input, output);
                    } catch (TransformerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

获取输出:

file:C:\Users\rimancha\Desktop\SaveDir\11439\11439.xml
C:/Users/rimancha/Desktop/SaveDir/output_xslt/outputC:\Users\rimancha\Desktop\SaveDir\11439\11439.xml created successfully? false
javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\Users\rimancha\Desktop\SaveDir\output_xslt\outputC:\Users\rimancha\Desktop\SaveDir\11439\11439.xml (The filename, directory name, or volume label syntax is incorrect)
    at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1220)
    at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1062)
    at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1270)
    at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1253)
    at com.yogs.learning.restclient.XsltFolderWise.displayDirectoryContents(XsltFolderWise.java:69)
    at com.yogs.learning.restclient.XsltFolderWise.main(XsltFolderWise.java:29)

但是我的文件路径正确。无法解决问题。 任何人都遇到这个问题,或者有这方面的解决方案? 是我的XSLT有一些问题,或者什么else.Please帮帮我!

1 个答案:

答案 0 :(得分:0)

这是您的错误: 首先,您查找文件

System.out.println("file:" + file.getCanonicalPath());

没关系,您已经知道了:

file:C:\Users\rimancha\Desktop\SaveDir\11439\11439.xml

但是这里您做错了什么

String OUTFILE = "C:/Users/rimancha/Desktop/SaveDir/output_xslt/output" + file;

我想,您想添加到目录文件名("C:/Users/rimancha/Desktop/SaveDir/output_xslt/output/123.TXT"),但是要添加完整的文件名("C:\Users\rimancha\Desktop\SaveDir\output_xslt\outputC:\Users\rimancha\Desktop\SaveDir\11439\11439.xml"

确定会引发异常,因为Windows上的文件名中不能包含“:”。

您的错误消息实际上告诉了它:“ java.io.FileNotFoundException:C:\ Users \ rimancha \ Desktop \ SaveDir \ output_xslt \ outputC:\ Users \ rimancha \ Desktop \ SaveDir \ 11439 \ 11439.xml(文件名,目录名称或卷标签语法不正确)“