Attribute lists

When a parser informs the application that an element start-tag has been encountered, it calls the startElement method, as described above. However, a start-tag may contain one or more attributes. It would not be possible to pass details of each attribute as individual parameters, because it is unknown in advance how many there could be. There is no theoretical limit to the number of attributes an element can contain.

The solution is for the parser to create a 'wrapper' object for all attribute details. This object must implement the AttributeList interface, which defines the following methods:

int     getLength();
String  getName(int i);
String  getType(int i);
String  getType(String name);
String  getValue(int i);
String  getValue(String name);

Note that in SAX 2.0, while this interface is still available, it is superseded by the Attributes interface, which adds support for namespaces.

Limitations

An attribute value may not be physically present in the start-tag, but may be supplied as a default value from the DTD instead. There is no way to distinguish between attributes defined explicitly and those that are supplied by the DTD. Also, the parser does not hold any information on attributes defined in the DTD that are both implied and happen not to appear in the start-tag.

It is therefore not possible, using the SAX API, to reconstruct a DTD from analysis of documents that conform to it.

Number of attributes

To ascertain how many attributes are present in this object, the getLength method is called. This method returns an integer value representing the number of attributes present (and a value of zero indicates that there are no attributes). Each attribute is identified by a simple index value. The first attribute has an index value of zero. It follows that the last attribute has an index value one less than the number returned by the getLength method.

Attribute name

To discover the name of one of the attributes, the getName method is called, along with its index value. This example retrieves the name of the last attribute:

String lastAttribute = null;
int totalAtts = atts.getLength();

if ( totalAtts > 0 )
       lastAttribute = atts.getName(totalAtts - 1);

Attribute value

Similarly, to get the value of an attribute, the getValue method is called. Unsurprisingly, the attribute value with a given index number matches the attribute name with the same index number.

The following example retrieves the value of the attribute with the name extracted in the previous example:

lastAttValue = atts.getValue(totalAtts - 1);

Attribute type

When a DTD is in use, each attribute is assigned a data type, such as CDATA, ID or NMTOKEN. The getType method returns this information. If the parser is unable to get this information from the DTD, perhaps because it is not a validating parser, then it substitutes the default type, 'CDATA'.

The following example partially reconstructs the original attribute list declaration (but makes a few unjustifiable assumptions about default values and requirement status):

public void startElement(String name, AttributeList atts)
{
  System.out.print( "<!ATTLIST " + name + " " );

  for( int i = 0; i < atts.getLength(); i++ )
  {
    System.out.print( atts.getName(i) + " " +
                      atts.getType(i) + " " +
                      "#IMPLIED "" +
                      atts.getValue(i) + "" 
");
  }

  System.out.print("> 
" );
}

Direct value access

When the application is only interested in a specific attribute, a simpler mechanism is provided for ascertaining its value. Instead of stepping through the attributes in sequence, looking for the one with the correct name, it is possible to simply request the value of a named attribute.

If the attribute is not present, a null value is returned.

The following example extracts the value of the Id attribute, regardless of which element is being processed:

public void startElement(String name, AttributeList atts)
{
  String ID = atts.getValue("Id");
}

The type of a named attribute can be discovered in the same way, though this seems to be a much less valuable feature. If the application already knows the name of the attribute it wants, it probably knows what data type its values conform to as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.6.75