1 9 10 package org.dom4j.samples.bean; 11 12 import org.dom4j.samples.SAXDemo; 13 14 import java.awt.Component ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.dom4j.Attribute; 19 import org.dom4j.Document; 20 import org.dom4j.Element; 21 import org.dom4j.bean.BeanDocumentFactory; 22 import org.dom4j.io.SAXReader; 23 24 31 public class BeanDemo extends SAXDemo { 32 33 public static void main(String [] args) { 34 run(new BeanDemo(), args); 35 } 36 37 public BeanDemo() { 38 } 39 40 public void run(String [] args) throws Exception { 41 if (args.length < 1) { 42 printUsage("<XML document URL>"); 43 return; 44 } 45 46 Document document = parse(args[0]); 47 process(document); 48 } 49 50 protected Document parse(String url) throws Exception { 51 SAXReader reader = new SAXReader(BeanDocumentFactory.getInstance()); 52 return reader.read(url); 53 } 54 55 protected void process(Document document) throws Exception { 56 List windows = document.selectNodes("//window"); 58 for (Iterator iter = windows.iterator(); iter.hasNext();) { 59 Element element = (Element) iter.next(); 60 Object window = element.getData(); 61 if (window instanceof Component ) { 62 Component component = (Component ) window; 63 component.setVisible(true); 64 } 65 66 println("found element: " + element); 67 println("found window: " + window); 68 } 69 70 println(""); 71 println("Now lets find all the fonts..."); 72 73 List fonts = document.selectNodes("//@font"); 74 for (Iterator iter = fonts.iterator(); iter.hasNext();) { 75 Attribute font = (Attribute) iter.next(); 76 println("found font: " + font.getData()); 77 } 78 79 } 80 81 } 82 83 121 | Popular Tags |