Java Reference
In-Depth Information
<xs:element name="email" type="xs:string"/>
<xs:element name="phone" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:complexType>
</xs:schema>
You can modify the
customer
example as follows:
To Modify the
customer
Example to Generate Java Entity Classes from an Existing XML
Schema Definition
1.
Create a JAXB binding to generate the entity Java classes from the schema defini-
tion. For example, in NetBeans IDE, follow these steps:
a.
Right click on the
customer
project and select New > Other...
b.
Under the XML folder, select JAXB Binding and click Next.
c.
In the Binding Name field, type
CustomerBinding
.
d.
Click Browse and choose the
.xsd
file from your file system.
e.
In the Package Name field, type
customer.xml
.
f. Click Finish.
This procedure creates the
Customer
class, the
Address
class, and some JAXB
auxiliary classes in the package
customer.xml
.
2. Modify the
CustomerService
class as follows:
a. Replace the
customer.data.*
imports with
customer.xml.*
im-
ports and import the
JAXBElement
and
ObjectFactory
classes:
import customer.xml.Customer;
import customer.xml.Address;
import customer.xml.ObjectFactory;
import javax.xml.bind.JAXBElement;
b. Replace the return type of the
getCustomer
method:
public JAXBElement<Customer> getCustomer(
@PathParam("id") String customerId) {
...
return new ObjectFactory().createCustomer(customer);
}