An entity in XML is something which holds data. A storage unit may be a part of a file, or the file itself, or a database record, or any other item which can hold data. A XML document with a prolog and a root element is called a document entity. Thus basically every XML document has at least one entity.

In XML the data can be inserted three ways: directly, predefined entities, or entity reference. 

Predefined Entities

This is discussed in earlier part of tutorial but given here again for the completeness of the topic. All the characters in the key board can be added as the XML content directly. But there are 5 reserved words which cannot be added as parsable data. They are:

Predefined entities
Character References
& &
< &lt;
> &gt;
&quot;
‘  &apos;

The &entity_nameis used in document instead of the symbols are it may be understood by parsers as XML tags.

Example with predefined entities in XML

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE root[

<!ELEMENT root (proverb)>

    <!ELEMENT proverb ANY>

]>

<root>

    <proverb>&quot;Make hay while sunshines&quot;</proverb>

</root>

The output from above example will be:

<root>

<proverb>"Make hay while sunshine"</proverb>

</root>

Entity references

The entity references can be added in two ways to a document. One way is as a numeric character reference which refers to a character or as an entity reference which refers to a series of characters. The numeric character is the Unicode character set of the character represented in hex or decimal. Entity reference is the user defined series of characters.

Syntax for entity references in XML

<!ENTITY entityName entityValue>

Example with entity references in XML

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE root[

    <!ELEMENT root (line,lang)>

    <!ELEMENT line ANY>

    <!ELEMENT lang ANY>

    <!ENTITY film "Harry Potter series">

]>

<root>

         

      <!—Entity reference reference -->

    <line>Favourite cinema is &film;</line>

 

      <!—Character reference -->

    <lang>&#x03A0; value is 22.7</lang>

</root>

The output from above example will be:

<root>

<line>Favourite cinema is Harry Potter series</line>

<lang>Π value is 22.7</lang>

</root>

 

›› go to examples ››