gpt4 book ai didi

java - 如何使用java获取目录及其子目录中多个文件的相同xml元素值?

转载 作者:行者123 更新时间:2023-12-01 09:59:27 24 4
gpt4 key购买 nike

  • 我有一个目录及其子目录,其中有许多 xml 文件。
  • XML 文件可以在不同的目录中具有相同的名称。
  • 但它有一个 xml 元素,并且对于所有文件来说都是非常唯一的,即使它拥有相同的 xml 文件名。
  • 现在我想读取所有 xml 元素并将其存储到列表中及其 xml 文件名和目录路径。
  • 它有一个公共(public)根目录,例如它可以有以下根路径
    D:\test\28-4-2016\BPG\8451835_1\ItemFile\1461819815710_19\ftp\content-providers\bpl-e\data\incoming

下面我提供了我的代码片段。

File fXmlFile = new File(path_received);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList2=doc.getElementsByTagName("CE:DOI");
if(nList2.getLength()>=1)
{
for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
Node nNode4 = nList2.item(temp2);

if (nNode4.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement1 = (Element) nNode4;
issn_value[temp2]=eElement1.getTextContent();
}
}
}

------------代码结束--------------
现在我只获取单个数据。任何人都可以帮助我按照我的上述要求实现这一目标。

提前致谢

------------------当前错误的堆栈跟踪:------------------------ ---

java.io.FileNotFoundException: E:\project_new\ttest\CBS_v47i4_e.xml (系统找不到指定的文件) 在 java.io.FileInputStream.open( native 方法) 在 java.io.FileInputStream.(来源未知) 在 java.io.FileInputStream.(来源未知) 在 sun.net.www.protocol.file.FileURLConnection.connect(来源未知) 在 sun.net.www.protocol.file.FileURLConnection.getInputStream(来源未知) 在 com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(来源未知) 在 com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.defineDocVersion(来源未知) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(来源未知) 在 com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(来源未知) 在 com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(来源未知) 在 com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(来源未知) 在 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(来源未知) 在 javax.xml.parsers.DocumentBuilder.parse(来源未知) 在 ttest.FileDemo.main(FileDemo.java:46)

最佳答案

我不太清楚你想要实现什么目标。但在我看来,您想要扫描文件夹中的 .xml 文件,并从所有这些 .xml 文件中获取特定元素并将其存储到列表中。

下面是一个示例程序,它扫描文件夹中的 .xml 文件并存储所有将“Element : path”字符串放入列表中。您可以使用 StringTokenizer 来分隔它们。还可以使用 StringTokenizer 将文件名与文件路径分开。

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class FileDemo {

// main method
public static void main(String[] args) {

File f = null;
try {
// create new file
String root = "C:\\temp";
f = new File(root);

//shall accept all files in directories and subdirectories
List<File> files = (List<File>) FileUtils.listFiles(f, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);

ArrayList<String> issn_valueLst = new ArrayList<>();

for (File fXmlFile : files) {
// prints filename and directory name
if(accept(fXmlFile.getName(), ".xml")){
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList2=doc.getElementsByTagName("CE:DOI");
if(nList2.getLength()>=1)
{
for (int temp2 = 0; temp2 < nList2.getLength(); temp2++) {
Node nNode4 = nList2.item(temp2);

if (nNode4.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement1 = (Element) nNode4;
issn_valueLst.add(eElement1.getTextContent()+"-"+fXmlFile.getAbsolutePath());
}
}
}
}
}
// }
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
}


public static boolean accept( String name, String str) {
return name.toLowerCase().endsWith(str.toLowerCase());
}
}

希望这有帮助!

关于java - 如何使用java获取目录及其子目录中多个文件的相同xml元素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36931494/

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