DOM guide: Working with elements

From COLLADA Public Wiki
Revision as of 18:01, 23 March 2007 by Elf (talk | contribs) (Accessing Element Data: just editing, first pass)
Jump to: navigation, search

daeElement is the base class for all of the COLLADA Object Model classes. The daeElement class uses the Reflective Object System (ROS) to provide considerable functionality without the need to use the generated subclass.

Determining an Element's Type

There are three approaches to determining the type of a daeElement:

  • Using the daeElement::getElementType method, which returns an enumerated value respresenting the element's type. The values returned by this function are in the dom/domTypes.h header. This is the easiest approach.
  • Using the daeElement::getTypeName method, which returns a daeString containing the type name. You can compare this string with the constant strings defined in dom/domConstants.h to determine the type.
  • By comparing _meta. The _meta is a pointer to a daeMetaElement, which is the class that contains all of the ROS information for an element type. By doing a pointer comparison on daeElement::getMeta and the generated class's static dom*::_Meta, it is possible to find the element type. This is the approach that the daeSafeCast method uses to ensure type safety.

NOTE: The daeSafeCast and daeElement::getElementType approaches are newer additions to the COLLADA DOM. When looking at older source code using the DOM, expect to see the older methods used frequently for type checking.

Accessing Element Data

The easier way to access element data is to cast the daeElement to its strongly typed subclass. At times, it might not be possible to do the cast or it might be more convenient not to cast. The daeElement class provides methods to access and manipulate data directly for such situations.

Navigating the Object Model

daeElement provides access to the document it belongs to with the getDocument method. The getDocumentURI method is a helper method if all you need is the name of the document that the element belongs to.

daeElement::getParentElement returns the parent element, which allows you to traverse up the tree to the root. If daeElement::getParentElement returns NULL, the element is the root of the tree. If the element belongs to a document then it is safe to assume that the element is a domCOLLADA element.

daeElement::getChildren populates an array with the children of an element. This allows you to iterate over all the children of an element and traverse down the element hierarchy. This array is ordered the same as it would be if the element were written to a COLLADA instance Document. This array is the same as the _contents array, if an element's type has a _contents array.

Accessing Attributes