문제

I have a little Problem with my code. I want to load an XML-File extract the Data out of the File, e.g. the different objects and their attributes. My thoughts are selecting the file over a Filechooser, get the with information with a XMLparser using JDOM and then saving the objects somewhere, e.g. a database. Now I got problem with reading in the selected file. My code looks like that, for better understanding:

public class xmlparser {

public static void main (String args[]) throws Exception
{       
    JFileChooser chooser = new JFileChooser();
    File f = new File("C:/x");
    chooser.setCurrentDirectory(f);
    chooser.setFileFilter(new OnlyXML());
    chooser.showOpenDialog(null);
    String path = f.getPath().toString();


    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(new File(path));
        document.normalize();
        System.out.println(document.getFirstChild().getTextContent());
    }
    catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}   
}

After compiling I receive this failure message:

[Fatal Error] :1:1: Content ist nicht zulässig in Prolog.
org.xml.sax.SAXParseException; systemId: file:/C:/x/; lineNumber: 1; 
columnNumber: 1; Content ist nicht zulässig in Prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at XMLParser.xmlparser.main(xmlparser.java:36)
at startmenu.Startmenu$2.mouseClicked(Startmenu.java:78)

There is a failure in Line 36 and Line 78. Line 36: I am sure it is something with the selected file, but I dont know what. To line 78: I call the xmlparser-class out of the startmenu-class, after a button is clicked and I dont understand why it is a mistake?

Here the code-part of it:

xmlparser x = new xmlparser();
x.main(null);

Just a normal call. I hope somebody can help and thank you very much :)

도움이 되었습니까?

해결책

I think that your problem is that you're not using the File chosen by the user, but the varaible f that correspond to the root folder for the JFileChooser. You should probably do something like that in your code :

 File choosenFile = chooser.getSelectedFile();

This will get the selected file of the JFileChooser. It's been a while since I've worked with that class so you will probably need to read a bit of the documentation here.

After you have the selected file, you can parse the file, and instead of creating a new File, you can re-use the file variable :

  Document document = docBuilder.parse(choosenFile);

I don't know if this will solved the problem, comment if you need anymore help.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top