Home arrow Java & J2EE arrow Page 8 - Integrating XML with J2EE

XML Schemas - Java

In this article, Martin Bond discusses XML and its associated APIs and standards, and how XML can be used to create flexible structured data that is inherently portable. This excerpt is from chapter (Day) 16 of Teach Yourself J2EE in 21 Days, second edition, by Martin Bond, et. al. (Sams, ISBN: 0672325586)

  1. Integrating XML with J2EE
  2. Benefits and Characteristics of XML
  3. Structure and Syntax of XML
  4. Structure of an XML Document
  5. Well-formed XML Documents
  6. Namespaces
  7. Element Type Declarations
  8. XML Schemas
  9. Parsing XML
  10. Parsing XML Using SAX
  11. Document Object Model (DOM) Parser
  12. Modifying a DOM Tree
  13. Java Architecture for XML Binding
  14. Summary
By: Sams Publishing
Rating: starstarstarstarstar / 23
November 16, 2004

print this article



As has been already stated, DTDs have some limitations:

  • A DTD cannot define type information other than characters.

  • DTDs were not designed to support namespaces and, although it is possible to add namespaces to a DTD, how to do so is beyond the scope of this book.

  • DTDs are not easily extended.

  • You can only have one DTD per document, so you cannot have different definitions of an element in a single document and have them validated with a DTD.

  • The syntax for DTDs is not XML. Tools and developers must understand the DTD syntax as well as XML.

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:

  • Specifying the structure and constraints on an XML document

  • A way of defining data types, including a set of pre-defined types

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.

Tip - The World Wide Web Consortium Web site provides access to a number of XML schema tools, including XML schema browsers and validators. These tools can be found at http://www.w3.org/XML/Schema.

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" 
<xsd:element name="jobSummary">
<xsd:element name="job" type="jobType" 
minOccurs="0" maxOccurs="unbounded"/>
<xsd:complexType name="jobType">
<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:attribute name="customer" type="xsd:string" use="required"/>
<xsd:attribute name="reference" type="xsd:string" use="required"/>

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

  • string Any combination of characters

  • integer Whole number

  • float Floating point number

  • boolean true/false or 1/0

  • date yyyy-mm-dd

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:

  • Portability of data

  • Portability of code

  • Ease of use

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:

  • Simple API for XML Parsing (SAX) for parsing XML as a stream.

  • Document Object Model (DOM) to build an in-memory tree representation of an XML document.

  • XML Stylesheet Language Transformations (XSLT) to control the presentation of the data and convert it to other XML documents or to other formats, such as HTML. XLST is covered on Day 17, "Transforming XML Documents."

JAXP also provides namespace support, allowing you to work with multiple XML documents that might otherwise cause naming conflicts.

Note - Because of the increasing use and importance of XML, JAXP is now incorporated into J2SE 1.4; previously it was available only in J2EE 1.3 or as a separate Java extension.

This chapter is from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond et. al. (Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.

>>> More Java & J2EE Articles          >>> More By Sams Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: