1 9 10 package org.dom4j.samples.jaxb; 11 12 import java.io.File ; 13 import java.math.BigDecimal ; 14 import java.util.Calendar ; 15 import java.util.Iterator ; 16 17 import org.dom4j.Document; 18 import org.dom4j.Element; 19 import org.dom4j.io.OutputFormat; 20 import org.dom4j.jaxb.JAXBModifier; 21 import org.dom4j.jaxb.JAXBReader; 22 import org.dom4j.jaxb.JAXBWriter; 23 import org.dom4j.test.primer.Items; 24 import org.dom4j.test.primer.ObjectFactory; 25 import org.dom4j.test.primer.PurchaseOrder; 26 import org.dom4j.test.primer.PurchaseOrders; 27 import org.dom4j.test.primer.USAddress; 28 29 34 public class JAXBDemo { 35 36 private File outputDir = new File ("build/test/"); 37 38 public static void main(String [] args) { 39 JAXBDemo demo = new JAXBDemo(); 40 demo.init(); 41 42 demo.demoRead(); 43 demo.demoReadPrune(); 44 45 demo.demoWrite(); 46 47 demo.demoModify(); 48 demo.demoModifyWrite(); 49 } 50 51 public void init() { 52 outputDir.mkdirs(); 53 } 54 55 public void demoRead() { 56 try { 57 File inputFile = new File ("xml/jaxb/primer.xml"); 58 59 JAXBReader jaxbReader = new JAXBReader("org.dom4j.test.primer"); 60 61 System.out 62 .println("Fetched PurchaseOrders using JAXBObjectHandler:"); 63 jaxbReader.addObjectHandler("/purchaseOrders/purchaseOrder", 64 new PurchaseOrderHandler()); 65 Document doc = jaxbReader.read(inputFile); 66 67 System.out.println("Fetched PurchaseOrders using DOM4J document:"); 68 Iterator orderIt = doc.selectNodes("/purchaseOrders/purchaseOrder") 69 .iterator(); 70 while (orderIt.hasNext()) { 71 Element elem = (Element) orderIt.next(); 72 73 System.out.println("Order - id:" + elem.attributeValue("id") 74 + ", date:" + elem.attributeValue("orderDate")); 75 } 76 77 System.out.println("Document:"); 78 System.out.println(doc.asXML()); 79 System.out.println(); 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } 83 } 84 85 public void demoReadPrune() { 86 try { 87 File inputFile = new File ("xml/jaxb/primer.xml"); 88 89 JAXBReader jaxbReader = new JAXBReader("org.dom4j.test.primer"); 90 91 System.out 92 .println("Fetched PurchaseOrders using JAXBObjectHandler:"); 93 jaxbReader.setPruneElements(true); 94 jaxbReader.addObjectHandler("/purchaseOrders/purchaseOrder", 95 new PurchaseOrderHandler()); 96 Document doc = jaxbReader.read(inputFile); 97 98 System.out.println("Pruned document:"); 99 System.out.println(doc.asXML()); 100 System.out.println(); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 } 104 } 105 106 public void demoWrite() { 107 try { 108 ObjectFactory factory = new ObjectFactory(); 109 110 PurchaseOrders orders = factory.createPurchaseOrders(); 111 112 PurchaseOrder order = factory.createPurchaseOrder(); 114 115 USAddress billTo = factory.createUSAddress(); 116 billTo.setCity("Cambridge"); 117 billTo.setCountry("US"); 118 billTo.setName("Robert Smith"); 119 billTo.setState("MA"); 120 billTo.setStreet("8 Oak Avenue"); 121 billTo.setZip(new BigDecimal (12345)); 122 order.setBillTo(billTo); 123 124 USAddress shipTo = factory.createUSAddress(); 125 shipTo.setCity("Cambridge"); 126 shipTo.setCountry("US"); 127 shipTo.setName("Alice Smith"); 128 shipTo.setState("MA"); 129 shipTo.setStreet("123 Maple Street"); 130 shipTo.setZip(new BigDecimal (12345)); 131 order.setShipTo(shipTo); 132 133 Calendar orderDate = Calendar.getInstance(); 134 orderDate.set(2004, 06, 30); 135 order.setOrderDate(orderDate); 136 137 Items items = factory.createItems(); 138 order.setItems(items); 139 140 orders.getPurchaseOrder().add(order); 141 142 PurchaseOrder order2 = factory.createPurchaseOrder(); 144 145 USAddress billTo2 = factory.createUSAddress(); 146 billTo2.setCity("Cambridge"); 147 billTo2.setCountry("US"); 148 billTo2.setName("Robert Smith"); 149 billTo2.setState("MA"); 150 billTo2.setStreet("8 Oak Avenue"); 151 billTo2.setZip(new BigDecimal (12345)); 152 order2.setBillTo(billTo2); 153 154 USAddress shipTo2 = factory.createUSAddress(); 155 shipTo2.setCity("Cambridge"); 156 shipTo2.setCountry("US"); 157 shipTo2.setName("Alice Smith"); 158 shipTo2.setState("MA"); 159 shipTo2.setStreet("123 Maple Street"); 160 shipTo2.setZip(new BigDecimal (12345)); 161 order2.setShipTo(shipTo2); 162 163 Calendar orderDate2 = Calendar.getInstance(); 164 orderDate2.set(2004, 06, 30); 165 order2.setOrderDate(orderDate2); 166 167 Items items2 = factory.createItems(); 168 order2.setItems(items2); 169 170 orders.getPurchaseOrder().add(order2); 171 172 File outputFile = new File (outputDir, "jaxbWrite.xml"); 173 174 JAXBWriter jaxbWriter = new JAXBWriter("org.dom4j.test.primer", 175 OutputFormat.createPrettyPrint()); 176 jaxbWriter.setOutput(outputFile); 177 178 jaxbWriter.startDocument(); 179 jaxbWriter.write(orders); 180 jaxbWriter.endDocument(); 181 } catch (Exception e) { 182 e.printStackTrace(); 183 } 184 } 185 186 public void demoModify() { 187 try { 188 File inputFile = new File ("xml/jaxb/primer.xml"); 189 190 JAXBModifier jaxbReader = new JAXBModifier("org.dom4j.test.primer"); 191 jaxbReader.addObjectModifier("/purchaseOrders/purchaseOrder", 192 new PurchaseOrderDateModifier()); 193 Document doc = jaxbReader.modify(inputFile); 194 195 System.out.println("Modified document:"); 196 System.out.println(doc.asXML()); 197 System.out.println(); 198 } catch (Exception e) { 199 e.printStackTrace(); 200 } 201 } 202 203 public void demoModifyWrite() { 204 try { 205 File inputFile = new File ("xml/jaxb/primer.xml"); 206 207 File outputFile = new File (outputDir, "testModifyWrite.xml"); 208 209 JAXBModifier jaxbModifier = new JAXBModifier( 210 "org.dom4j.test.primer", OutputFormat.createPrettyPrint()); 211 jaxbModifier.setPruneElements(true); 212 jaxbModifier.setOutput(outputFile); 213 jaxbModifier.addObjectModifier("/purchaseOrders/purchaseOrder", 214 new PurchaseOrderDateModifier()); 215 Document doc = jaxbModifier.modify(inputFile); 216 217 System.out.println("Pruned modified document:"); 218 System.out.println(doc.asXML()); 219 System.out.println(); 220 } catch (Exception e) { 221 e.printStackTrace(); 222 } 223 } 224 225 } 226 227 265 | Popular Tags |