Java
  Home arrow Java arrow Page 7 - Integrating XML with J2EE
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
JAVA

Integrating XML with J2EE
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Integrating XML with J2EE - Element Type Declarations
    (Page 7 of 14 )

    The DTD defines every element in the XML document with element type declarations. Each element type declaration takes the following form:

    <!ELEMENT name ( content ) >

    For example, for the jobSummary XML document in Listing 16.4, the jobSummary root element is defined as

    <!ELEMENT jobSummary ( job* )>

    The * sign indicates that the jobSummary element may consist of zero or more job elements. There are other symbols used to designate rules for combining elements and these are listed in Table 16.3.

    Table 16.3 Occurrence Characters Used in DTD Definitions

    Character

    Meaning

    *

    Zero or more (not required)

    +

    One or more (at least one required)

    ?

    Element is optional (if present can only appear once)

    |

    Alternate elements

    ()

    Group of elements


    The following defines an XML job element that must include one location, an optional description, and at least one skill:

    <!ELEMENT job (location, description?, skill+)>

    Defining the Element Content

    Elements can contain other elements, or content, or have elements and content. The jobSummary element, in Listing 16.4, contains other elements but no text body; whereas the location element has a text body but does not contain any elements.

    To define an element that has a text body, use the reference #PCDATA (Parsed Character DATA). For example, the location element in Listing 16.4 is defined by

    <!ELEMENT location (#PCDATA)>

    An element can also have no content (the <br> tag in HTML is such an example). This tag would be defined with the EMPTY keyword as

    <!ELEMENT br EMPTY>

    You will also see elements defined with contents of ANY. The ANY keyword denotes that the element can contain all possible elements, as well as PCDATA. The use of ANY should be avoided. If your data is so unstructured that it cannot be defined explicitly, there probably is no point in creating a DTD in the first place.

    Defining Attributes

    In Listing 16.4, the job element has two attributes—customer and reference. Attributes are defined in an ATTLIST that has the following form:

    <!ATTLIST element attribute type default-value>

    The element is the name of the element and attribute is the name of the attribute. The type defines the kind of attribute that is expected. A type is either one of the defined constants described in Table 16.4, or it is an enumerated type where the permitted values are given in a bracketed list.

    Table 16.4 DTD Attribute Types

    Type

    Attribute Is a...

    CDATA

    Character string.

    NMTOKEN

    Valid XML name.

    NMTOKENS

    Multiple XML names.

    ID

    Unique identifier.

    IDREF

    An element found elsewhere in the document. The value for IDREF must match the ID of another element.

    ENTITY

    External binary data file (such as a gif image).

    ENTITIES

    Multiple external binary files.

    NOTATION

    Helper program.


    The ATTLIST default-value component defines a value that will be used if one is not supplied. For example

    <!ATTLIST button visible (true | false) "true").

    defines that the element button has an attribute called visible that can be either true or false. If the attribute is not supplied, because a default value is supplied, it will be set to be true.

    The default-value item can also be used to specify that the attribute is #REQUIRED, #FIXED, or #IMPLIED. The meaning of these values is given in Table 16.5.

    Table 16.5 DTD Attribute Default Values

    Default Value

    Meaning

    #REQUIRED

    Attribute must be provided.

    #FIXED

    Effectively a constant declaration. The attribute must be set to the given value or the XML is not valid.

    #IMPLIED

    The attribute is optional and the processing application is allowed to use any appropriate value if required.


    Example DTD

    Listing 16.7 is the DTD for the jobSummary XML document. Create the DTD in a file called jobSummary.dtd in the same directory as your jobSummary XML document.

    Listing 16.7 DTD for jobSummary XML

    <!ELEMENT jobSummary (job*)>
    <!ELEMENT job (location, description, skill+)>
    <!ATTLIST job customer CDATA #REQUIRED>
    <!ATTLIST job reference CDATA #REQUIRED>
    <!ELEMENT location (#PCDATA)>
    <!ELEMENT description (#PCDATA)>
    <!ELEMENT skill (#PCDATA)>

    Don't forget to add the following line to the jobSummary XML at line 2 (following the PI):

    <!DOCTYPE jobSummary SYSTEM "jobSummary.dtd">

    View the jobSummary.xml document in your XML browser or other XML validator.

    If the browser cannot find the DTD, it will generate an error. Edit jobSummary.xml, remove the customer attribute, and check that your XML validator generates an appropriate error (such as "Required attribute 'customer' is missing").

    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

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway