Document Example
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
public class XMLPrinter {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java XMLPrinter URL1 URL2...");
}
SAXBuilder builder = new SAXBuilder();
// start parsing...
for (int i = 0; i < args.length; i++) {
// command line should offer URIs or file names
try {
Document doc = builder.build(args[i]);
System.out.println("*************" + args[i] + "*************");
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, System.out, "UTF-8");
}
catch (JDOMException e) { // indicates a well-formedness or other error
System.out.println(args[i] + " is not well formed.");
System.out.println(e.getMessage());
}
catch (IOException e) { // shouldn't happen beacuse System.out eats exceptions
System.out.println(e.getMessage());
}
}
}
}