Example 1. Enumerating the children of an Element using sibling pointers
This example illustrates how to enumerate each child node of a DOM_Element node. This is a more efficient method of enumerating children than indexing each with the DOM_NodeList(elem->childNodes, idx) technique.
for (child = elem->firstChild; child; child = child->nextSibling) { if (child->nodeType == DOM_COMMENT_NODE) { printf("comment: %s\n", child->nodeValue); } }In addition to the functions provided by the DOM_Node interface this interface provides additional functions mainly for manipulating attributes.
The DOM specifications require support for entity references which may result in the childNodes of an attribute containing a potentially complex subtree of DOM nodes. DOMC currently has very weak support for entity references and as a result attributes will never have children. The default module for loading and storing XML documents uses the Expat XML parser which expands entity references by default. Expat recently added support for parsing external entities but DOMC does not yet use this functionalty.
The DOM_Element_getAttribute function
Description
#include <domc.h> DOM_String *DOM_Element_getAttribute(DOM_Element *this, DOM_String *name);
The DOM_Element_setAttribute function
Description
#include <domc.h> void DOM_Element_setAttribute(DOM_Element *this, DOM_String *name, DOM_String *value);
The DOM_Element_removeAttribute function
Description
#include <domc.h> void DOM_Element_removeAttribute(DOM_Element *this, DOM_String *name);
Note the W3C specifications require that an attribute with a default DTD value should automatically be repopulated if a user supplied attribute value is removed. DOMC does not support default DTD values. This function will simply remove the attribute regarless of whether or not the attribute value was specified in the DTD. The Expat XML parser just released support for parsing external entities. DOMC will likely support external entities and default attribute values in a future version.
The DOM_Element_getAttributeNode function
Description
#include <domc.h> DOM_Attr *DOM_Element_getAttributeNode(DOM_Element *this, DOM_String *name);
The DOM_Element_setAttributeNode function
Description
#include <domc.h> DOM_Attr *DOM_Element_setAttributeNode(DOM_Element *this, DOM_Attr *newAttr);
The DOM_Element_removeAttributeNode function
Description
#include <domc.h> DOM_Attr *DOM_Element_removeAttributeNode(DOM_Element *this, DOM_Attr *oldAttr);
DOMC does not support default DTD values. This function will remove an attribute regarless of whether or not a default attribute value was specified in the DTD.
The DOM_Element_getElementsByTagName function
Description
#include <domc.h> DOM_NodeList *DOM_Element_getElementsByTagName(DOM_Element *this, DOM_String *name);
After the DOM_NodeList object will no longer to be used it must be freed with the DOM_Document_destroyNodeList function with a free_nodes parameter of 0 (the nodes in this list should not be freed or all other references to them will be invalid).
The DOM_Element_hasAttribute function
Description
#include <domc.h> int DOM_Element_hasAttribute(DOM_Element *this, DOM_String *name);