Java
  Home arrow Java arrow Page 8 - Integrating XML with J2EE
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Integrating XML with J2EE
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2004-11-16


    Table of Contents:
  • Integrating XML with J2EE
  • Benefits and Characteristics of XML
  • Structure and Syntax of XML
  • Structure of an XML Document
  • Well-formed XML Documents
  • Namespaces
  • Element Type Declarations
  • XML Schemas
  • Parsing XML
  • Parsing XML Using SAX
  • Document Object Model (DOM) Parser
  • Modifying a DOM Tree
  • Java Architecture for XML Binding
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Integrating XML with J2EE - XML Schemas
    ( Page 8 of 14 )

    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" 
           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

    • 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 Articles          >>> More By Sams Publishing
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek