1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 26 import nu.xom.Attribute; 27 import nu.xom.Builder; 28 import nu.xom.Document; 29 import nu.xom.Element; 30 import nu.xom.Elements; 31 import nu.xom.ParsingException; 32 33 34 45 public class TreePrinter { 46 47 public static void main(String [] args) { 48 49 if (args.length == 0) { 50 System.out.println("Usage: java nu.xom.samples.TreePrinter URL"); 51 return; 52 } 53 54 Builder builder = new Builder(); 55 56 try { 57 Document doc = builder.build(args[0]); 58 Element root = doc.getRootElement(); 59 listChildren(root); 60 } 61 catch (ParsingException ex) { 63 System.out.println(args[0] + " is not well-formed."); 64 System.out.println(ex.getMessage()); 65 } 66 catch (IOException ex) { 67 System.out.println(ex); 68 } 69 70 } 71 72 73 public static void inspect(Element element) { 75 76 if (element.getParent() != null) { 77 System.out.println(); 80 } 81 82 String qualifiedName = element.getQualifiedName(); 83 System.out.println(qualifiedName + ":"); 84 85 String namespace = element.getNamespaceURI(); 86 if (!namespace.equals("")) { 87 String localName = element.getLocalName(); 88 String uri = element.getNamespaceURI(); 89 String prefix = element.getNamespacePrefix(); 90 System.out.println(" Local name: " + localName); 91 System.out.println(" Namespace URI: " + uri); 92 if (!"".equals(prefix)) { 93 System.out.println(" Namespace prefix: " + prefix); 94 } 95 } 96 for (int i = 0; i < element.getAttributeCount(); i++) { 97 Attribute attribute = element.getAttribute(i); 98 String name = attribute.getQualifiedName(); 99 String value = attribute.getValue(); 100 System.out.println(" " + name + "=\"" + value + "\""); 101 } 102 103 for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) { 104 String additional = element.getNamespacePrefix(i); 105 String uri = element.getNamespaceURI(additional); 106 System.out.println( 107 " xmlns:" + additional + "=\"" + uri + "\""); 108 } 109 110 } 111 112 public static void listChildren(Element current) { 113 114 inspect(current); 115 Elements children = current.getChildElements(); 116 for (int i = 0; i < children.size(); i++) { 117 listChildren(children.get(i)); 118 } 119 120 } 121 122 } 123 | Popular Tags |