Comment Example
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.util.*;
public class CommentReader {
public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
for (int i = 0; i < args.length; i++) {
try {
// Read the entire document into memory
Document doc = builder.build(args[i]);
List content = doc.getMixedContent();
Iterator iterator = content.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Comment) {
Comment c = (Comment) o;
System.out.println(c.getText());
System.out.println();
}
else if (o instanceof Element) {
processElement((Element) o);
}
}
}
catch (JDOMException e) {
System.err.println(e);
e.getRootCause().printStackTrace();
}
}
} // end main
// note use of recursion
public static void processElement(Element element) {
List content = element.getMixedContent();
Iterator iterator = content.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Comment) {
Comment c = (Comment) o;
System.out.println(c.getText());
System.out.println();
}
else if (o instanceof Element) {
processElement((Element) o);
}
} // end while
}
}