1 17 package org.apache.ws.jaxme.examples.misc.address; 18 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.StringWriter ; 23 24 import javax.xml.bind.JAXBContext; 25 import javax.xml.bind.JAXBException; 26 import javax.xml.bind.Marshaller; 27 import javax.xml.bind.Unmarshaller; 28 29 import org.apache.ws.jaxme.test.misc.address.Address; 30 import org.xml.sax.InputSource ; 31 32 33 37 public class AddressPrinter { 38 public static Address getAddress(InputSource pSource) throws JAXBException { 39 JAXBContext context = JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.address"); 40 Unmarshaller unmarshaller = context.createUnmarshaller(); 41 return (Address) unmarshaller.unmarshal(pSource); 42 } 43 44 public static String getAddressAsString(Address pAddress) throws JAXBException { 45 StringWriter sw = new StringWriter (); 46 JAXBContext context = JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.address"); 47 Marshaller marshaller = context.createMarshaller(); 48 marshaller.marshal(pAddress, sw); 49 return sw.toString(); 50 } 51 52 public static void main(String [] args) throws Exception { 53 File f = new File ("Address.xml"); 54 InputSource isource = new InputSource (new FileInputStream (f)); 55 isource.setSystemId(f.toURL().toString()); 56 Address addr = getAddress(isource); 57 58 61 if (addr.getName() == null) { 62 System.out.println("Loaded address without name."); 63 } else { 64 System.out.println("Loaded address " + addr.getName().getLast() + 65 ", " + addr.getName().getFirst() + "."); 66 } 67 System.out.println("Details:" + getAddressAsString(addr)); 68 } 69 } 70 | Popular Tags |