JDOM Example

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.util.*;
import java.io.IOException;


public class JDOMWordCount {

  public static void main(String[] args) {
  
    if (args.length == 0) {
      System.out.println("Usage: java JDOMWordCount URL"); 
    } 
      
    SAXBuilder builder = new SAXBuilder();
     
    // command line should offer URIs or file names
    try {
      Document doc = builder.build(args[0]);
      Element root = doc.getRootElement();
      int numWords = countWordsInElement(root);
      System.out.println(numWords + " words");
    
    }
    catch (JDOMException ex) { // indicates a well-formedness error
      System.out.println(args[0] + " is not well formed.");
      System.out.println(ex.getMessage());
    }
    catch (IOException ex) { // indicates an I/O error
      System.out.println("Could not read " + args[0] + " due to an I/O error.");
      System.out.println(ex.getMessage());
    } 
 
  }

  public static int countWordsInElement(Element element) {
    
    int numWords = 0;
    
    List children = element.getContent();
    Iterator iterator = children.iterator();
    while (iterator.hasNext()) {
      Object o = iterator.next();
      if (o instanceof Text) {
        numWords += countWordsInString((Text) o);
      } 
      else if (o instanceof Element) {
        // note use of recursion
        numWords += countWordsInElement((Element) o); 
      } 
    }
    
    return numWords;  
    
  }

  private static int countWordsInString(Text text) {
    
    String s = text.getText();
    if (s == null) return 0;
    s = s.trim();
    if (s.length() == 0) return 0;
    
    StringTokenizer st = new StringTokenizer(s);
    return st.countTokens();
    
  }

}
% java JDOMWordCount hotcop.xml
16 words

Previous | Next | Top | Cafe con Leche

Copyright 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified July 14, 2000