Entity References

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Appendix B.  XML: A Quick Tour


Before discussing entity references, let's consider three situations:

  • You want to enter the > sign in the XML document. You will not be able to enter the > sign directly, because the XML parser will treat it as a markup, and the XML processor will take great offense when it finds a > hanging around in the XML document. You need a mechanism to escape the > character, so that it can be entered in the XML document.

  • Suppose you are creating an XML document describing car parts, and you need to specify the name of a product that is still under development. At the time you created the document, suppose the name of the product was Wonder Writer. After some time, the legal department figured out that there was a problem with the name and decided to change it to Wonderful Writing Gadget. This will mean that you will need to change the name of the product in multiple places in your XML document. This leads to both wasted time and the possibility of error creeping in. Therefore, there should be a mechanism by which a change in the name can be reflected in all the places at one shot and without errors.

  • You want to provide reference to a GIF file to show the image for the product, or include content from another XML file.

To answer these situations, XML provides support for entities. Three types of entities are possible:

  • Predefined entities

  • Internal entities

  • External entities

Predefined Entities

The predefined entities help you solve the situation faced in the first scenario. XML provides a set of predefined entities for the characters that an XML parser otherwise treats as markup characters. These are shown in Table B.1.

Table B.1. Predefined Entities

Character

Entity Reference

&

& or &

'

' or '

>

> or >

<

&lt; or &#38;#60;

"

&quot; or &#34;

Internal Entities

Internal entities help you solve the second type of scenario. The internal entities are declared in the following fashion:

<!ENTITY name "replacement text"> 

This declaration is done in the DTD of the XML document. After the internal entity has been declared, you can use it like a character reference, by using the string &name;. For example, consider the entity declarations declared as part of the internal DTD subset in the CarParts.xml file:

<!ENTITY  companyname "Heaven Car Parts (TM)"> <!ENTITY  companyweb "http://carpartsheaven.com"> 

In this example, the companyname entity represents Heaven Car Parts (TM), and the companyweb entity represents the Web site address of the company.

Let's now look at how it has been used in the XML document:

<supplier name="&companyname;" URL="&companyweb;"> </supplier> 

Notice that in the supplier element, the name attribute has the value &companyname; and the URL attribute has the value &companyweb;. When the XML parser parses this file, it will replace all occurrences of &companyname; and &companyweb; with the text they represent.

External Entities

External entities are those entities that refer to content outside the XML document. This can be another XML file or a binary file, such as GIFs or JPEGs. Using external entities, you can meet the need of referencing information that lies outside of the XML document.

For example, suppose you want to include the contents of a file called descriptions.xml. You can do so by declaring an external entity in the following way:

<!ENTITY descriptions SYSTEM "descriptions.xml"> 

The SYSTEM argument tells the XML parser that the file is in the local file system. It is also possible to specify a URI. So, if the descriptions.xml was located in www.carheaven.com/descriptions/description.xml, the entity declaration would have been

<!ENTITY descriptions SYSTEM "http://www.carheaven.com/descriptions/description.xml"> 

Although creating external entities, instead of SYSTEM, you can also use the PUBLIC argument. Using the PUBLIC argument specifies that the file being referenced is available for widespread use and therefore publicized.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/app02lev1sec10]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

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