-2

I have got two XML files from two different databases, but they has got the same infos. One of them has got VSReports and the other Jasperreports(JavaScript). I has to convert the XML file from VSReports into the Jasperreports. The only programming language I am allowed to use is java.

I am already stucked when I try to read in a xml file with my code.

    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:

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
  • Possible duplicate of [What is the best/simplest way to read in an XML file in Java application?](https://stackoverflow.com/questions/428073/what-is-the-best-simplest-way-to-read-in-an-xml-file-in-java-application) – Anurag Srivastava Jul 30 '19 at 08:43
  • 4
    and what is "a xml javascript file" ? – Stultuske Jul 30 '19 at 08:46
  • @Stultuske it's more or less specified in the question "a xml file that was written with javascript". Which is still irrelevant, of course. – Federico klez Culloca Jul 30 '19 at 08:47
  • 1
    an xml file is an xml file regardless if it is written by hand, by C#, by PHP, by javascript, or even by the blunt crayon called java – Jaromanda X Jul 30 '19 at 08:49
  • "to **convert a xml file to a xml file** that was written with javascript" - xml is xml, nothing needs to be done here. – Amongalen Jul 30 '19 at 08:51
  • 1
    Your question makes no sense. Try reframing your question, for others to better understand. – Sajib Acharya Jul 30 '19 at 08:51

3 Answers3

0

you are trying to open a file without giving it an absolute path, it will only work if the file is in the current working directory

0

Based on your description, it seems you didn't pass the full name of the xml file to your function. 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.

Mike
  • 185
  • 6
0

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. To convert a File object to a Path, just use its toPath() method:

Path path = chooser.getSelectedFile().toPath();
solid.py
  • 2,782
  • 5
  • 23
  • 30
Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14