简体   繁体   English

如何将xml文件转换为用java编写的javascript文件的xml?

[英]How can I convert a xml file to a xml written with javascript file with java?

I have got two XML files from two different databases, but they has got the same infos.我有来自两个不同数据库的两个 XML 文件,但它们有相同的信息。 One of them has got VSReports and the other Jasperreports(JavaScript).其中一个有 VSReports,另一个有 Jasperreports(JavaScript)。 I has to convert the XML file from VSReports into the Jasperreports.我必须将 XML 文件从 VSReports 转换为 Jasperreports。 The only programming language I am allowed to use is java.我被允许使用的唯一编程语言是 java。

I am already stucked when I try to read in a xml file with my code.当我尝试使用我的代码读取 xml 文件时,我已经陷入困境。

    import javax.swing.*;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;

    public class InputBox {
        public static void main(String[] args) {
            Pfad();
        }
    //opens JFileChooser
        public static void Pfad() {

                JFileChooser chooser = new JFileChooser();
                int rueckgabeWert = chooser.showOpenDialog(null);

                if (rueckgabeWert == JFileChooser.APPROVE_OPTION) {
                    System.out.println("Die zu öffnende Datei ist: "
                            + chooser.getSelectedFile().getName());
                }

                Path path = Paths.get(chooser.getSelectedFile().getName());
            String content = null;
            try {
                content = Files.readString(path, Charset.defaultCharset());
            } catch (IOException e) {
                e.printStackTrace();
            }
//System.out.println the content of the file
            System.out.println(content);
        }
    }

It works really fine with a txt file, but when I try a XML file it comes to a error:它适用于 txt 文件,但当我尝试使用 XML 文件时,它会出现错误:

java.nio.file.NoSuchFileException: 123.xml
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
    at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:370)
    at java.base/java.nio.file.Files.newByteChannel(Files.java:421)
    at java.base/java.nio.file.Files.readAllBytes(Files.java:3205)
    at java.base/java.nio.file.Files.readString(Files.java:3283)
    at InputBox.Pfad(InputBox.java:26)
    at InputBox.main(InputBox.java:10)
null

你试图打开一个文件而不给它一个绝对路径,只有当文件在当前工作目录中时它才会工作

Based on your description, it seems you didn't pass the full name of the xml file to your function.根据您的描述,您似乎没有将 xml 文件的全名传递给您的函数。 So try所以试试

    File f = chooser.getSelectedFile();
    String path = f.getAbsolutePath + f.getName();
    try {
            content = Files.readString(path, Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }

Hope this help.希望这有帮助。

As said, File#getName() loses the directory part of the path, so it will only find a file if it is in the current directory.如前所述, File#getName() 丢失了路径的目录部分,因此它只会找到当前目录中的文件。 To convert a File object to a Path , just use its toPath() method:要将File对象转换为Path ,只需使用其toPath()方法:

Path path = chooser.getSelectedFile().toPath();

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

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