Logo Search packages:      
Sourcecode: libupnp4 version File versions  Download package

EXPORT_SPEC int ixmlElement_setAttribute ( IXML_Element element,
const DOMString  name,
const DOMString  value 
)

Adds a new attribute to an Element.

If an attribute with the same name already exists in the element, the attribute value will be updated with the new value parameter. Otherwise, a new attribute is inserted into the element.

Returns:
An integer representing of the following:
  • IXML_SUCCESS: The operation completed successfully.
  • IXML_INVALID_PARAMETER: Either element, name, or value is NULL.
  • IXML_INVALID_CHARACTER_ERR: name contains an illegal character.
  • IXML_INSUFFICIENT_MEMORY: Not enough free memory exists to complete the operation.
Parameters:
element  [in] The Element on which to set the attribute.
name  [in] The name of the attribute.
value  [in] The value of the attribute. Note that this is a non-parsed string and any markup must be escaped.

Definition at line 106 of file element.c.

References _IXML_Node::firstAttr, ixmlAttr_free(), ixmlDocument_createAttributeEx(), ixmlElement_setAttributeNode(), _IXML_Element::n, _IXML_Node::nextSibling, _IXML_Node::nodeName, _IXML_Node::nodeValue, _IXML_Node::ownerDocument, and Parser_isValidXmlName().

{
      IXML_Node *attrNode;
      IXML_Attr *newAttrNode;
      short errCode = IXML_SUCCESS;

      if (element == NULL || name == NULL || value == NULL) {
            errCode = IXML_INVALID_PARAMETER;
            goto ErrorHandler;
      }

      if (Parser_isValidXmlName(name) == FALSE) {
            errCode = IXML_INVALID_CHARACTER_ERR;
            goto ErrorHandler;
      }

      attrNode = element->n.firstAttr;
      while (attrNode != NULL) {
            if (strcmp(attrNode->nodeName, name) == 0) {
                  /* Found it */
                  break;
            } else {
                  attrNode = attrNode->nextSibling;
            }
      }

      if (attrNode == NULL) {
            /* Add a new attribute */
            errCode = ixmlDocument_createAttributeEx(
                  (IXML_Document *)element->n.ownerDocument, name, &newAttrNode);
            if (errCode != IXML_SUCCESS) {
                  goto ErrorHandler;
            }

            attrNode = (IXML_Node *)newAttrNode;
            attrNode->nodeValue = strdup(value);
            if (attrNode->nodeValue == NULL) {
            ixmlAttr_free(newAttrNode);
            errCode = IXML_INSUFFICIENT_MEMORY;
                  goto ErrorHandler;
            }

            errCode = ixmlElement_setAttributeNode(element, newAttrNode, NULL);
            if (errCode != IXML_SUCCESS) {
                  ixmlAttr_free(newAttrNode);
                  goto ErrorHandler;
            }
      } else {
            if (attrNode->nodeValue != NULL) {
                  /* Attribute name has a value already */
                  free(attrNode->nodeValue);
            }
            attrNode->nodeValue = strdup(value);
            if (attrNode->nodeValue == NULL) {
                  errCode = IXML_INSUFFICIENT_MEMORY;
            }
      }

ErrorHandler:
      return errCode;
}


Generated by  Doxygen 1.6.0   Back to index