Ilavarasan Jayaraman
Ilavarasan Jayaraman

Reputation: 303

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

Below I have provided my code snippet.

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();
       } 
               }
  }

-----------Code ends--------------
Now I am getting the single data only. can any one help me to achieve this as per my above requirement.

Thanks in Advance

------------------Stack trace of present error : --------------------------

java.io.FileNotFoundException: E:\project_new\ttest\CBS_v47i4_e.xml (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source) at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at ttest.FileDemo.main(FileDemo.java:46)

Upvotes: 0

Views: 1062

Answers (1)

mattyman
mattyman

Reputation: 351

It is not very clear to me what you are you trying to achieve. But it seems to me that you want to scan a folder for .xml files and get a particular element from all these .xml files and store it into the list.

Below is a sample program that scans a folder for .xml files and stores all "Element : path" strings into a list. You can use a StringTokenizer to separate them. Also using StringTokenizer you can separate the file name from the file path.

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());
    }
}

Hope this helps!

Upvotes: 1

Related Questions