0
林上的文件夾中讀取XML文件,然後讓其中的XML文件中順便說一句,這是我的XML文件看起來像獲取文件夾
<?xml version="1.0" encoding="UTF-8"?> -<Config> <Name>MyName</Name> ..... </Config>
的
<name> MyName </name>
程序工作的XML元素的值
我想要得到的<name>
這個值是我現在
File directory = new File(txtSource.getText());
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.getName().startsWith("Config")) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(file.toString()));
Element rootElement = document.getDocumentElement();
rootElement.getAttribute("name");
System.out.println(rootElement);
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我想我的代碼,這是這是不對的輸出
[Config: null]
假設這個[示例](https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/)是有一個解決方案,並且具有更好地解決您的問題。 –
Yoow @RajithPemabandu謝謝我沒有看到谷歌上的鏈接 – user123040569