XML attributes

Attributes are additional metadata on an element. For example, in the following example an element has the color and size attributes:

<myelement color="blue" size="huge">

Every type instance contains its attributes, which are XmlSimpleValue instances specified by a name (a QName).

Each XmlElement object contains the following methods and properties related to attributes of the element:

  • AttributeNames property – Gets a set of QName objects. The property type is java.util.Set<QName>.
  • getAttributeSimpleValue(QName) – Get an attribute simple value by its name, specified as a QName. Returns a XmlSimpleValue object. An alternative method signature takes a String instead of a QName. This method assumes an empty namespace.
  • getAttributeValue(QName) : String – Get attribute value by its name, specified as a QName. Returns a String object. An alternative method signature takes a String instead of a QName. This method assumes an empty namespace.
  • setAttributeSimpleValue(QName , XmlSimpleValue) – Set an attribute simple value by its name (as a QName) and its value (as a XmlSimpleValue object). An alternative method signature takes a String instead of a QName. This method assumes an empty namespace.
  • setAttributeValue(QName , String) – Set attribute value by its name (as a QName) and its value (as a XmlSimpleValue object). An alternative method signature takes a String instead of a QName. This method assumes an empty namespace.

Using the previous example, the following code gets and sets the attributes:

myelement.setAttributeValue("color", XmlSimpleValue.makeStringInstance("blue"))
var s = myelement.getAttributeValue("size")

If you use XSDs for your elements, for typical use, do not use these methods. Instead, use the shortcuts that Gosu provides. These shortcuts provide a natural and concise syntax for getting and setting attributes.

See also