1 56 57 import java.io.*; 58 import java.util.*; 59 60 import org.jdom.*; 61 import org.jdom.filter.ElementFilter; 62 import org.jdom.input.*; 63 import org.jdom.output.*; 64 65 68 public class DescendantDemo { 69 70 public static void main(String [] args) throws Exception { 71 if (args.length != 1) { 72 System.err.println("Usage: java DescendantDemo [web.xml]"); 73 return; 74 } 75 76 SAXBuilder builder = new SAXBuilder(); 77 Document doc = builder.build(args[0]); 78 79 System.out.println("All content:"); 80 Iterator itr = doc.getDescendants(); 81 while (itr.hasNext()) { 82 Content c = (Content) itr.next(); 83 System.out.println(c); 84 } 85 86 System.out.println(); 87 System.out.println("Only elements:"); 88 itr = doc.getDescendants(new ElementFilter()); 89 while (itr.hasNext()) { 90 Content c = (Content) itr.next(); 91 System.out.println(c); 92 } 93 94 System.out.println(); 95 System.out.println("Everything that's not an element:"); 96 itr = doc.getDescendants(new ElementFilter().negate()); 97 while (itr.hasNext()) { 98 Content c = (Content) itr.next(); 99 System.out.println(c); 100 } 101 102 System.out.println(); 103 System.out.println("Only elements with localname of servlet:"); 104 itr = doc.getDescendants(new ElementFilter("servlet")); 105 while (itr.hasNext()) { 106 Content c = (Content) itr.next(); 107 System.out.println(c); 108 } 109 110 System.out.println(); 111 System.out.println( 112 "Only elements with localname of servlet-name or servlet-class:"); 113 itr = doc.getDescendants(new ElementFilter("servlet-name") 114 .or(new ElementFilter("servlet-class"))); 115 while (itr.hasNext()) { 116 Content c = (Content) itr.next(); 117 System.out.println(c); 118 } 119 120 System.out.println(); 121 System.out.println("Remove elements with localname of servlet:"); 122 itr = doc.getDescendants(new ElementFilter("servlet")); 123 while (itr.hasNext()) { 124 itr.next(); 125 itr.remove(); 126 } 127 128 XMLOutputter outp = new XMLOutputter(); 129 outp.output(doc, System.out); 130 } 131 } 132 | Popular Tags |