Java & J2EE Page 8 - Integrating XML with J2EE |
As has been already stated, DTDs have some limitations:
To address these issues, the XML Schema structure definition mechanism was developed by the W3C to fulfill the role of DTDs while addressing the previously listed limitations. XML Schemas are XML documents. The XML Schema standard is split into two parts:
Because it is a more powerful and flexible mechanism than DTDs, the syntax for defining an XML schema is slightly more involved. An example of an XML schema for the jobSummary XML shown in Listing 16.4 can be seen in Listing 16.8.
Listing 16.8 XML Schema for Job Agency JobSummary XML Document <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:element name="jobSummary"> <xsd:complexType> <xsd:sequence> <xsd:element name="job" type="jobType" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="jobType"> <xsd:sequence> <xsd:element name="location" type="xsd:string"/> <xsd:element name="description" type="xsd:string"/> <xsd:element name="skill" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute name="customer" type="xsd:string" use="required"/> <xsd:attribute name="reference" type="xsd:string" use="required"/> </xsd:complexType> </xsd:schema> The first thing to notice is that this schema exists within a namespace as defined on the second line. The string xsd is used by convention for a schema namespace, but any prefix can be used. Schema Type Definitions and Element and Attribute Declarations Elements that have sub-elements and/or attributes are defined as complex types. In addition to complex types, there are a number of built-in simple types. Examples of a few simple types are
A complex type element (one with attributes or sub-elements) has to be defined in the schema and will typically contain a set of element declarations, element references, and attribute declarations. Listing 16.8 contains the definition for the job tag complex type, which contains three elements (location, description, and skill) and two attributes (customer and reference). In a schema, like a DTD, elements can be made optional or required. The job element in Listing 16.8 is optional because the value of the minOccurs attribute is 0. In general, an element is required to appear when the value of minOccurs is 1 or more. Similarly, the maximum number of times an element can appear is determined by the value of maxOccurs. This value can be a positive integer or the term unbounded to indicate there is no maximum number of occurrences. The default value for both the minOccurs and the maxOccurs attributes is 1. If you do not specify the number of occurrences, the element must be present and must occur only once. Element attributes can be declared with a use attribute to indicate whether the element attribute is required, optional, or even prohibited. There are more aspects to schemas than it is possible to cover in this book. Visit the WC3 Web site (http://www.w3.org) for more information on XML schemas and all other aspects of XML. J2EE Support for XML XML is portable data, and the Java platform is portable code. Add Java APIs for XML that make it easy to use XML and, together, you have the ideal combination:
The J2EE platform bundles all these advantages together. Enterprises are rapidly discovering the benefits of using J2EE for developing Web Services that use XML for the dissemination and integration of data; particularly because XML eases the sharing of legacy data both internally among departments and with other enterprises. J2EE includes the Java API for XML Processing (JAXP) that makes it easy to process XML data with applications written in Java. JAXP embraces the parser standards:
JAXP also provides namespace support, allowing you to work with multiple XML documents that might otherwise cause naming conflicts.
blog comments powered by Disqus |