DOM guide: Working with documents
Creating the DAE Object
To use the COLLADA DOM, your client code needs to include the following header files:
#include <dae.h> #include <dom/domCOLLADA.h>
To use a specific effect profile, such as <profile_COMMON> or <profile_CG>, you must include the header for that profile. For example:
A useful header to include is
dom/domConstants.h. This provides constants for strings that are commonly used in the DOM. These strings include all the COLLADA element names and element type names. Many examples to follow use these constants.
After including the appropriate headers, your application needs to create a DAE object. The following example shows a DAE object created on the heap (using
operator new) and accessed as a pointer.
DAE *collada_dom = new DAE(); // use the collada_dom variable delete collada_dom;
You could also create the DAE object globally or on the stack.
URIs and File Paths
Loading COLLADA Documents
DAE::load method to read an existing COLLADA document into the runtime database. When you load a document, the COLLADA DOM uses the document URI as the name of the document when searching or doing other operations where the document needs to be identified. In this example, the name of the document created by the load process is “file:///input_url_name.dae”, the same as the URI it was loaded from:
daeInt error = collada_dom->load("file:///input_url_name.dae");
A common mistake is to pass a Windows or Linux file path to the
DAE::save methods. Like COLLADA itself, the DOM uses URIs exclusively to reference resources or files. A file path needs to be converted to a file scheme URI before being passed to the COLLADA DOM. Below are some examples of converting a file path to a URI.
Example Description File Path URI Windows absolute path C:\folder\file.dae file:///C:/folder/file.dae Windows relative path folder\file.dae folder/file.dae UNC path \\remoteMachine\folder\file.dae file://///remoteMachine/folder/file.dae Linux absolute path /folder/file.dae file:///folder/file.dae Linux relative path folder/file.dae folder/file.dae
DAE::load method returns:
DAE_OKif the load is successful.
DAE_ERR_COLLECTION_ALREADY_EXISTSiff a document with the same name, or the same document, has already been loaded.
DAE_ERR_BACKEND_IOfor other errors encountered during load.
Note: The terms "collection" and "document" mean the same thing in the COLLADA DOM interface and documentation.
Unloading COLLADA Documents
DAE::unload() method to unload a document from the runtime database. The document to be unloaded is specified by its document URI name. This method returns either:
DAE_ERR_COLLECTION_DOES_NOT_EXISTif the document does not exist in the runtime database.
((ANDY: What does unload do? When/why would you do it? (Is it just a memory thing?) ))
Saving COLLADA Documents
After you have completed any changes to the elements in the database, you can write the contents of the database to a new URL using the DAE::saveAs() method to save the specific collection. You can use either the document name or the document index to choose which collection to save:
daeInt error = daeObject->saveAs("output_url_name.xml", "file:///input_url_name.xml");
To save the document to the same location that it was loaded from, use the DAE:save() method. This method has only two arguments; the first is either the document name or the document index. The second is an optional flag that specifies whether to overwrite any existing document. This flag defaults to true.
DAE::saveAs has three arguments. The first is the URI to save the document as. The second is either the document name or the document index. If this argument is omitted, the COLLADA DOM defaults to saving the first document that is currently in the runtime database. The last argument is an optional flag that specifies whether to overwrite any existing document. This flag defaults to true.
Note: The COLLADA DOM XML parser does not fully validate the data. It detects some types of errors in XML input but not all of them. You may want to run new COLLADA data through an XML validator before loading it into the DOM. If you are developing programs that write COLLADA data, you should run a validator on the output of your programs during testing.