gpt4 book ai didi

java - 系统找不到文件指定的文档 document = documentBuilder()

转载 作者:行者123 更新时间:2023-12-01 12:33:00 25 4
gpt4 key购买 nike

在此

Document document = documentBuilder.parse(filename);

我正在尝试通过缓冲输入流读取一组Xml文件。当我在命令提示符中输入文件时,出现错误 As System Cannot find the specified File 。有人可以建议如何从这里继续

import java.io.File;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLFile {

public static void main(String argv[]) {
try {

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
BufferedReader bufferRead = new BufferedReader(
new InputStreamReader(System.in));
String s = bufferRead.readLine();
File dir = new File(s);
String[] children = dir.list();
if (children == null) {
} else {
for (int i = 0; i < children.length; i++) {
String filename = children[i];
Document document = documentBuilder.parse(filename);
System.out.println(filename);
NodeList employees = document
.getElementsByTagName("connection");
for (int q = 0; q < employees.getLength(); q++) {
System.out.println(employees.getLength());
Node employee = employees.item(q);
NamedNodeMap attribute = employee.getAttributes();
Node nodeAttr = attribute.getNamedItem("server");
nodeAttr.setTextContent("aventador.am.lilly.com:1530");
Node nodeAttr1 = attribute.getNamedItem("service");
Node nodeAttr2 = attribute.getNamedItem("port");
nodeAttr1.setTextContent("tst806");
nodeAttr2.setTextContent("");

}
System.out.println(filename);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory
.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(
filename));
transformer.transform(domSource, streamResult);
System.out.println("The XML File was ");
}
}

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
} catch (Exception sae1) {
sae1.printStackTrace();
}
}
}

最佳答案

在 else block 中,您尝试执行 Document document = documentBuilder.parse(filename);

文件名参数应该是一个 URI - 意思是完整的文件路径。但是您只传递文件名,因此无论您的输入目录是什么,DocumentBuilder 都会在当前目录中查找 XML 文件。更改以下行

字符串文件名=children[i];

字符串文件名=dir.getAbsolutePath()+File.separator+children[i];

这应该可以解决问题。

关于java - 系统找不到文件指定的文档 document = documentBuilder(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25794980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com