繁体   English   中英

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

[英]How to get the same xml element value of multiple files in a directories and its subdirectories using java?

  • 我有一个带有子目录的目录,并且有许多xml文件。
  • XML文件可以在不同目录中以相同的名称存在。
  • 但是它具有一个xml元素,即使它具有相同的xml文件名,它对于所有文件也是非常唯一的。
  • 现在,我想读取所有xml元素并将其与xml文件名和目录路径一起存储到LIST中。
  • 它具有公共的根目录,例如可以具有以下根路径
    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(本机方法),位于java.io.FileInputStream。(未知源) .io.FileInputStream。(未知源),位于sun.net.www.protocol.file.FileURLConnection.connect(未知源),位于sun.net.www.protocol.file.FileURLConnection.getInputStream(com.sun)。 com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(未知源)的org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(未知源)com.sun.org.apache.xerces的org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(未知源) com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(未知源)位于com.sun.org.apache.xerces.internal.parsers.XMLParser中。 com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(未知源)com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(未知源) ce),位于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());
    }
}

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM