<title section="Resin : XML Tutorial : ">
Attributes
</title>

<p/>Attributes are special children of elements.  They attach a set of
&lt;key, value> pairs to each element.

<section title="Adding Attributes">

<p/>Adding attributes uses the <var/setAttribute()/> method.

<example title="Resulting XML">
&lt;top a="1" b="2"/>
</example>

<example title="Adding Attributes with the DOM">
// Create a new document
Document doc = builder.newDocument();

// Create the top element
Element top = doc.createElement("top");

// Add an attribute
top.setAttribute("a", "1");
top.setAttribute("b", "2");
</example>

</section>

<section title="Retrieving Attributes">

<p/>Retrieving attributes uses the <var/getAttribute()/> method of the
<var/Element/>.  Since the <var/getAttribute()/> method is specific to
the <var/Element/> and not to <var/Node/>, you'll need to cast it properly.

<p/>If an attribute is missing, the value of <var/getAttribute()/> is the
empty string, <var/""/>, not <var/null/>.  So you can safely use
<var/equals()/> to compare attribute values without checking for <var/null/>.

<example title="Retrieving Attributes">
Node ptr;
for (ptr = top.getFirstChild();
     ptr != null && ! ptr.getNodeName().equals("b");
     ptr = ptr.getNextSibling()) {
}

Element elt = (Element) ptr;

if (elt == null)
  System.out.println("No element b");
else if (elt.getAttribute("id").equals("")) {
  System.out.println("b has no 'id' attribute");
else
  System.out.println("b id=" + elt.getAttribute("id"));
</example>

</section>

