我正在尝试解析xml文件,到目前为止,这是我写的内容。
public class XMLTest {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
File xmlFile = new File("D:\\workspace\\sudoku baby\\sudoku.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
}
它不断吐出找不到文件的异常,像这样
Exception in thread "main" java.io.FileNotFoundException: D:\workspace\sudoku baby\sudoku.xml (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:110)
at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:654)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:150)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:860)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206)
at testingXML.XMLTest.main(XMLTest.java:14)
` 我已经完全确定它是正确的文件路径,并且在进行一些测试后可以找到该文件。当我尝试解析该错误时。我不确定这里到底是什么问题,如果有人可以指出或帮助的话,那会很好。
答案 0 :(得分:0)
请尝试以下代码:
File xmlFile = new File(Paths.get("D:","workspace", "sudoku baby", "sudoku.xml").toString());