XML Concepts

Extensible Markup Language, or XML as it is popularly known, is a markup language similar to Hyper Text Markup Language (HTML) but far more powerful than HTML. XML is a simpler form of the Standardized Generalized Markup Language (SGML), which is the parent of all markup languages.

What makes XML different is that it is not a language but a "meta" language, that is, a language that in turn describes another language. Perhaps it will help to discuss this feature in the context of HTML. As any Web developer knows, HTML is a language with a predefined set of tags (defined in the HTML specification) that are embedded in a file called an HTML file that is rendered by a Web browser. XML, on the other hand, does not have any predefined tags. This means that developers are free to define their own custom tags using the XML language. Defining your own tags is no great help, though, if the grammar, that is, how the tags should be defined and interpreted, is not present. Hence, you need to define the grammar of the XML file in a separate file. There are two ways to define the structure of an XML file: Document Type Definitions (DTD) and XML schema.

Document Type Definitions (DTDs)

The Document Type Definition (DTD) is the document that contains the details of the structure of the XML file. The structure is defined in terms of elements, attributes, comments, and so on. DTDs essentially define the grammar of the XML file. They give an idea of how the contents, that is, the custom tags in the XML, should be interpreted. DTDs are also used to ensure that the contents of the XML file are correct, that is, that no tags are out of place or missing. A sample DTD is given here:

 <!DOCTYPE sams_book_catalogue [  <!ELEMENT catalogue_item_id(#PCDATA)> <!ELEMENT book (title, price, author+, publisher,)> <!ELEMENT title (#CDATA)> <!ELEMENT price (#CDATA)> <!ELEMENT author (#CDATA)> <!ELEMENT publisher (#CDATA)> ]> 

The use of DTDs is now deprecated. The preferred way to define XML grammar is to use XML schemas.

XML Schema

An XML schema consists of components such as type definitions and element declarations that are similar to what is defined in a DTD. The difference between a DTD and an XML schema is that an XML schema is also an XML file. Similar to a DTD, an XML schema can be used for validating the contents of an XML file, as shown here:

 <xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">      <xsd:element name="sams_book_catalogue" type="catalogueType"/>     <xsd:element name="catalogue_item_id" type="xsd:string"/>     <xsd:complexType name="book">         <xsd:element name="title" type="xsd:string"/>         <xsd:element name="price" type="xsd:string"/>         <xsd:element name="author" type="xsd:string" minOccurs="1"                 maxOccurs="unbounded"/>         <xsd:element name="publisher" type="xsd:string"/>     </xsd:complexType> </xsd:schema> 

Having learned how XML grammar is defined, you may be wondering why you would want to define your own tags in the first place.

The answer lies in the use of XML in applications. The significant difference between XML and HTML is that, unlike HTML, which is used primarily for presentation purposes, XML finds use in data sharing because it provides an easy and flexible way to encapsulate data, and more importantly, describes the structure of the data. This "self-describing" ability of XML is what gives XML its flexibility.

Now take a quick look at the following simple XML file:

 <?xml version=1.0?>  <!DOCTYPE address_book SYSTEM "sams_book_catalogue.dtd"> <sams_book_catalogue>     <catalogue_item_id>1001</catalogue_item_id>     <book>         <title>Teach Yourself WebLogic Server in 21 Days</title>         <price>39.99</price>         <author>Mandar S. Chitnis</author>         <author>Lakshmi Ananthamurthy</author>         <publisher>SAMS Publishing</publisher>     </book>     <book>         ...     </book> </sams_book_catalogue> 

From this code snippet, you can see that there is a definite structure associated with the contents of an XML file. The contents of an XML file are data encapsulated within a set of tags. The tags can be either a start and end tag set or a single tag with an end tag marker within it. The tag elements can be recursive; that is, a set of tag elements can be contained within another set of tag elements. You have already seen how an XML file was used as a deployment descriptor file to register deployment information in the file.

For example, to register a Java servlet, you would register the servlet's deployment information within the <servlet></servlet> tags in the web.xml deployment descriptor file.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net