Java Reference
In-Depth Information
<circle positionx=30 positiony=50 radius=15/>
Now we have just one element defining a circle with all the data defined by attribute values. Where all
the data in a document is defined as attribute values it is described as attribute-normal .
An element can also contain a mixture of text and markup - so called mixed content - so we have a
further way in which we could define a circle in XML, like this:
<circle>
<position>
<x-coordinate>30</x-coordinate>
<y-coordinate>50</y-coordinate>
</position>
15
</circle>
Now the value for the radius just appears as text as part of the content of the <circle> element along
with the position element. The disadvantage of this arrangement is that it's not obvious what the text is
so some information about the structure has been lost compared to the previous example.
So which is the better approach, to go for attributes or elements? Well, it can be either, or both, if you see
what I mean. It depends on what the structure of the data is, how the XML is generated, and how it will be
used. One overriding consideration is that an attribute is a single value. It has no inner structure so anything
that does have substructure must be expressed using elements. Where data is essentially hierarchical,
representing family trees in XML for instance, you will want to use nested elements to reflect the structure of
the data. Where the data is serial or tabular, temperature and rainfall or other weather data over time for
instance, you may well use attributes within a series of elements within the root element.
If you are generating an XML document interactively using an editor, then readability is an important
consideration since poor readability will encourage errors. You will lean towards whatever makes the
editing easier - and for the most part elements are easier to find and edit than attributes. Attribute
values should be short for readability so this limits the sort of data that you can express as an attribute.
You probably would not want to see the soliloquy from Shakespeare's Hamlet appearing as an attribute
value for instance. That said, if the XML is computer generated and is not primarily intended for human
viewing, the choice is down to the most efficient way to handle the data in the computer. Attributes and
their values are readily identified in a program so documents are likely to make use of attributes
wherever the structure of the data does not require otherwise. We will see how this works out in practice
when we get to use the Java API for processing XML.
Whitespace and Readability
The indentation shown in the examples so far have been included just to provide you with visual cues to
the structure of the data. It is not required, and an XML processor will ignore the whitespace between
elements. When you are creating XML in an editor, you can use whitespace generally between elements
to present the XML document visually so that it is easier for a human reader to understand. Whitespace
can consist of spaces, tabs, carriage returns, and line feed characters. You can see that a circle expressed
without whitespace, as shown below, would be significantly less readable:
<circle><position><x-coordinate>30</x-coordinate><y-coordinate>50</y-
coordinate></position>15</circle>
Search WWH ::




Custom Search