KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 import javax.xml.bind.JAXBContext;
24 import javax.xml.bind.JAXBException;
25 import javax.xml.bind.Marshaller;
26
27 import org.apache.ws.jaxme.impl.JMMarshallerImpl;
28 import org.apache.ws.jaxme.test.misc.address.Address;
29 import org.apache.ws.jaxme.test.misc.address.ObjectFactory;
30 import org.apache.ws.jaxme.test.misc.address.AddressType.EmailDetailsType.EmailType;
31 import org.apache.ws.jaxme.test.misc.address.AddressType.PhoneDetailsType.PhoneType;
32
33
34 public class AddressCreator {
35     public static void writeAddress(Writer JavaDoc pWriter) throws JAXBException {
36         ObjectFactory f = new ObjectFactory();
37         
38         // Create the element:
39
Address addr = f.createAddress();
40         addr.setName(f.createAddressTypeNameType());
41         addr.getName().setFirst("Jane");
42         addr.getName().setLast("Doe");
43         addr.setPostal(f.createAddressTypePostalType());
44         addr.getPostal().setStreet("34 Main Street");
45         addr.getPostal().setCity("Boston");
46         addr.getPostal().setState("MA");
47         addr.getPostal().setZIP("02215");
48         addr.setEmailDetails(f.createAddressTypeEmailDetailsType());
49         
50         EmailType email = f.createAddressTypeEmailDetailsTypeEmailType();
51         email.setType("Private");
52         email.setEmailAddress("jdoe@yourcompany.com");
53         addr.getEmailDetails().getEmail().add(email);
54         email = f.createAddressTypeEmailDetailsTypeEmailType();
55         email.setType("Office");
56         email.setEmailAddress("josephdoe@mycompany.com");
57         addr.getEmailDetails().getEmail().add(email);
58         email = f.createAddressTypeEmailDetailsTypeEmailType();
59         email.setType("Office");
60         email.setEmailAddress("joseph.doe@mycompany.com");
61         addr.getEmailDetails().getEmail().add(email);
62         
63         addr.setPhoneDetails(f.createAddressTypePhoneDetailsType());
64         PhoneType phone = f.createAddressTypePhoneDetailsTypePhoneType();
65         phone.setType("Work");
66         phone.setPhoneNumber("555.6789");
67         addr.getPhoneDetails().getPhone().add(phone);
68         phone = f.createAddressTypePhoneDetailsTypePhoneType();
69         phone.setType("Fax");
70         phone.setPhoneNumber("555.1212");
71         addr.getPhoneDetails().getPhone().add(phone);
72         
73         // And save it into the file "Address.xml"
74
JAXBContext context = JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.address");
75         Marshaller marshaller = context.createMarshaller();
76         marshaller.setProperty(JMMarshallerImpl.JAXME_XML_DECLARATION, Boolean.FALSE);
77         marshaller.marshal(addr, pWriter);
78     }
79     
80     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
81         FileWriter JavaDoc fw = new FileWriter JavaDoc("Address.xml");
82         writeAddress(fw);
83         fw.close();
84     }
85 }
86
Popular Tags