• Get the Flash Player to see the slideshow.
  • Categories

  • Authors

  • Great Quotes

    Thirty spokes converge on a hub but it’s the emptiness that makes a wheel work — Lao Tzu - Tao Te Ching

Get in touch...

To have a chat about
your CMS needs...

Call us 0207 193 2014
or
Email us on

Read List of XML Files from Directory Tree in Java

March 2nd, 2009 by Michael Thomas

This 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;
}
 
}

One Response to “Read List of XML Files from Directory Tree in Java”

  1. tanguyr Says:

    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.

Leave a Reply

You must be logged in to post a comment.