Read List of XML Files from Directory Tree in Java
March 2nd, 2009 by Michael ThomasThis is a TeamSite 6.7.2/LiveSite 3.1 compliant mechanism that can be used to read all DCR files into a single XML files for further processing:
package com.littleforest.examples; import com.interwoven.livesite.dom4j.Dom4jUtils; import com.interwoven.livesite.external.ParameterHash; import com.interwoven.livesite.runtime.RequestContext; import com.interwoven.livesite.file.FileDALIfc; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.*; import org.dom4j.*; import java.util.Properties; /* Added for proxy use */ import com.interwoven.livesite.external.*; import java.io.IOException; public class xmlFileListContents { public Document fetchTree(RequestContext context) { //look in ALL directories Document doc = Dom4jUtils.newDocument(); Element fileElem = doc.addElement("Records"); FileDALIfc fileDAL = context.getFileDal(); String areaRelativeBasePath = context.getParameterString("BaseDirectory"); String absoluteBasePath = fileDAL.getRoot() + fileDAL.getSeparator() + areaRelativeBasePath; //recurse directories String[] directories = fileDAL.getChildDirectories(absoluteBasePath); int dirCount = directories.length; for(int k=0;k<dirCount;k++) { String absoluteDirPath = absoluteBasePath + fileDAL.getSeparator() + directories[k]; //recurse files in directories String[] files = fileDAL.getChildFiles(absoluteDirPath); int count = files.length; for(int j=0;j<count;j++) { String absoluteFilePath = absoluteDirPath + fileDAL.getSeparator() + files[j]; Document fileDCR = Dom4jUtils.newDocument(fileDAL.read(absoluteFilePath)) ; //add path as an attribute fileDCR.getRootElement().addAttribute("path",absoluteFilePath); // add the dcr to the result doc; fileElem.add(fileDCR.getRootElement().createCopy()) ; //optionally, may prefer path as an element //fileElem.addElement("../path").addText(absoluteFilePath); } } //ensure we have those in the Base too String[] baseFiles = fileDAL.getChildFiles(absoluteBasePath); int fCount = baseFiles.length; for(int i=0;i<fCount;i++) { String absoluteFilePath = absoluteBasePath + fileDAL.getSeparator() + baseFiles[i]; Document fileDCR = Dom4jUtils.newDocument(fileDAL.read(absoluteFilePath)) ; //add path as an attribute fileDCR.getRootElement().addAttribute("path",absoluteFilePath); // add the dcr to the result doc; fileElem.add(fileDCR.getRootElement().createCopy()) ; //optionally, may prefer path as an element //fileElem.addElement("../path").addText(absoluteFilePath); } return doc; } }

January 7th, 2010 at 7:34 pm
I’ve just been bitten by this, so i thought i’d leave a note for others: fileDAL.getChildDirectories(absoluteBasePath); is *not* recursive, it just loads the directories which are directly under absoluteBasePath. If you have DCRs that are more than one level below absoluteBasePath they will not be found.