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