XML documents can refer to entities defined in DTD. Woodstox provides support for replacing the entity reference with its corresponding value. The following program explores Woodstox’ feature of replacing entity references.

Create the employee.dtd as shown below.

In particular, note that the entity ‘commonAddress’ is defined with corresponding value ‘Beth Street, Cathy County, Dessert State’ (see line 6 below).

Create employee.xml as shown below.

Note that employee.dtd is referred from within the XML (see line 2 below).

Also note that the entity ‘&commonAddress;’ is referred within address element (see line 6 below)

Create TestReplaceEntityReference as shown below.

In particular, note the ‘disableReplaceEntityReference’ flag which controls whether entity reference will be replaced or not (see line 26-28 below).

Create the main method (see lines 43-49 below).

Run the ‘execute’ method by enabling replacement of entity references (see line 45 below).

Rerun the ‘execute’ method by disabling replacement of entity references (see line 48 below).

The output of the program demonstrating replacing of entity references is shown below. Note that in the first case, entity references were replaced (see line 5 below).

In the second case, entity references were not replaced (see lines 7-10 below).

Download Source Code for this Program

Leave a Comment