KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > examples > misc > address > AddressPrinter


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.examples.misc.address;
18
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.StringWriter JavaDoc;
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 JavaDoc;
31
32
33 /**
34  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
35  * @version $Id: AddressPrinter.java,v 1.3 2004/02/16 23:39:55 jochen Exp $
36  */

37 public class AddressPrinter {
38   public static Address getAddress(InputSource JavaDoc 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 JavaDoc getAddressAsString(Address pAddress) throws JAXBException {
45     StringWriter JavaDoc sw = new StringWriter JavaDoc();
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 JavaDoc[] args) throws Exception JavaDoc {
53     File JavaDoc f = new File JavaDoc("Address.xml");
54     InputSource JavaDoc isource = new InputSource JavaDoc(new FileInputStream JavaDoc(f));
55     isource.setSystemId(f.toURL().toString());
56     Address addr = getAddress(isource);
57
58     // A simpler variant might be:
59
// Address addr = unmarshaller.unmarshal(f);
60

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