The XML Side

   

The object model is one side of the equation; the other side is the XML file. In an ideal world, the XML file would match your object structure exactly and would be similar to the following:

 <?xml version='1.0'encoding='ISO-8859-1'?>    <Catalog>       <VisualProduct>          <Id>wp01</Id>          <Checked>false</Checked>          <Name>WhizBang Word Processor</Name>          <Image>images/wordprocessor.jpg</Image>       </VisualProduct>       <TextualProduct>          <Id>li04</Id>          <Checked>false</Checked>          <Title>WhizBang Bright Light</Title>          <Descriptions>             <Description>                <Language>EN</Language>                <Text>With power saving.</Text>             </Description>             <Description>                <Language>FR</Language>                <Text>Avec gestion d'nergie.</Text>             </Description>          </Descriptions>       </TextualProduct>    </Catalog> 

Crash Course on XML

XML stands for eXtensible Markup Language. Similar to HTML, it is a markup language developed by the World Wide Web Consortium (W3C).

The syntax for XML is similar to HTML syntax, so it looks familiar. However, the X in XML means that, unlike HTML, the language is not fixed.

Indeed, HTML has a fixed set of tags ( <BODY> , <TITLE> , <P> , <IMG> , and so on); the list of acceptable tags was published by the W3C.

XML has no built-in tags and it is up to you, the developer, to create the tags you need.

Therefore, whereas HTML tags carry presentation instructions (for example, <FONT> , <CENTER> , and <PRE> ), XML tags tend to be related to the structure of the information. For example, an address book will have tags such as <Name> , <Street> , and <Phone> .

In other words, XML tags don't tell you how the information should be presented onscreen (bold, italics, or centered) but rather what the information is. For example, the tag

 <Name>John Doe</Name> 

means that the person's name is John Doe.

The second major difference between XML and HTML is that XML enforces a very strict syntax. Without going in the details, note the following:

  • Elements must be enclosed in a start tag and an end tag. It is no longer possible to ignore the end tag. The following is an example:

     <Phone>513-744-7098</Phone> 
  • Empty elements (elements with no content) follow a special syntax, which looks similar to the following:

     <Email href="mailto:jdoe@emailaholic.com"/> 
  • Attribute values must be enclosed in double or single quotes.

For a comprehensive introduction to XML, I recommend you read my other book, XML by Example, published by Que.

However, that is the ideal case. In practice, the product information comes from the central database, so chances are the XML file will be closer to the database organization than to your object model. It is not unlikely that the file will look similar to Listing 1.7.

Obviously, because it is based on the same list of products, Listing 1.7 is not completely alien to your object model either. The major difference is that it doesn't have a VisualProduct or TextualProduct . In Listing 1.7, every entry is a product. Listing 1.7's structure is illustrated in Figure 1.4.

Listing 1.7 catalog.xml
 <?xml version='1.0'encoding='ISO-8859-1'?> <Catalog>    <Product id='wp01'checked='false'>       <Text>WhizBang Word Processor</Text>       <Image>images/wordprocessor.jpg</Image>    </Product>    <Product id='sf02'checked='false'>       <Text>WhizBang Safest Safe</Text>       <Image>images/safe.jpg</Image>    </Product>    <Product id='ca03'checked='false'>       <Text>WhizBang Good Calculator</Text>       <Image>images/calculator.jpg</Image>    </Product>    <Product id='li04'checked='false'>       <Text>WhizBang Bright Light</Text>       <Descriptions>          <Text xml:lang='EN'>With power saving.</Text>          <Text xml:lang='FR'>Avec gestion d'nergie.</Text>       </Descriptions>    </Product> </Catalog> 
Figure 1.4. XML document structure.

graphics/01fig04.gif

   


Applied XML Solutions
Applied XML Solutions
ISBN: 0672320541
EAN: 2147483647
Year: 1999
Pages: 142

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