1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Builder; 27 import nu.xom.Document; 28 import nu.xom.Element; 29 import nu.xom.Elements; 30 import nu.xom.ParsingException; 31 32 33 43 public class ElementLister { 44 45 public static void main(String [] args) { 46 47 if (args.length == 0) { 48 System.out.println("Usage: java nu.xom.samples.ElementLister URL"); 49 return; 50 } 51 52 Builder builder = new Builder(); 53 54 try { 55 Document doc = builder.build(args[0]); 56 Element root = doc.getRootElement(); 57 listChildren(root, 0); 58 } 59 catch (ParsingException ex) { 61 System.out.println(args[0] + " is not well-formed."); 62 System.out.println(ex.getMessage()); 63 } 64 catch (IOException ex) { 65 System.out.println(ex); 66 } 67 68 } 69 70 71 public static void listChildren(Element current, int depth) { 72 73 printSpaces(depth); 74 System.out.println(current.getQualifiedName()); 75 Elements children = current.getChildElements(); 76 for (int i = 0; i < children.size(); i++) { 77 listChildren(children.get(i), depth+1); 78 } 79 80 } 81 82 private static void printSpaces(int n) { 83 84 for (int i = 0; i < n; i++) { 85 System.out.print(' '); 86 } 87 88 } 89 90 } 91 | Popular Tags |