Difference between revisions of "DOM guide: Importing documents"
(New page: Be sure to read the section on creating documents first. It covers some important topics relevant to this section.) |
|||
Line 1: | Line 1: | ||
Be sure to read the section on [[DOM guide: Creating documents| creating documents]] first. It covers some important topics relevant to this section. | Be sure to read the section on [[DOM guide: Creating documents| creating documents]] first. It covers some important topics relevant to this section. | ||
+ | |||
+ | ==A simple example== | ||
+ | Let's begin with a simple example of reading some information from a Collada document. We'll open the document and print the ID of the first <node> we find. | ||
+ | |||
+ | DAE dae; | ||
+ | daeElement* root = dae.open("simpleImport.dae"); | ||
+ | if (!root) { | ||
+ | cout << "Document import failed.\n"; | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | We create the DAE object then call DAE::open to open a file called "simpleImport.dae". If there is no file of that name in the current directory, or the file failed to open for some other reason, then the DAE::open method will return null. We check for that and print an error message if opening the document failed. | ||
+ | |||
+ | daeElement* node = root->getDescendant("node"); | ||
+ | if (!node) | ||
+ | cout << "No nodes found\n"; | ||
+ | else | ||
+ | cout << "node id: " << node->getAttribute("id") << endl; | ||
+ | |||
+ | Here we use the daeElement::getDescendant method to do a breadth-first search through the xml element tree for an element with the given name. This method will return null if it couldn't find an element with a matching name, which we check for. If it did find a matching element we use the daeElement::getAttribute method to print the value of the 'id' attribute. | ||
+ | |||
+ | The complete code. | ||
+ | |||
+ | #include <iostream> | ||
+ | #include <dae.h> | ||
+ | #include <dom/domCOLLADA.h> | ||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | DAE dae; | ||
+ | daeElement* root = dae.open("simpleImport.dae"); | ||
+ | if (!root) { | ||
+ | cout << "Document import failed.\n"; | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | daeElement* node = root->getDescendant("node"); | ||
+ | if (!node) | ||
+ | cout << "No nodes found\n"; | ||
+ | else | ||
+ | cout << "node id: " << node->getAttribute("id") << endl; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | The simpleImport.dae document. | ||
+ | |||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <COLLADA xmlns="<nowiki>http://www.collada.org/2005/11/COLLADASchema</nowiki>" version="1.4.1"> | ||
+ | <asset> | ||
+ | <contributor/> | ||
+ | <created>2008-04-08T13:07:52-08:00</created> | ||
+ | <modified>2008-04-08T13:07:52-08:00</modified> | ||
+ | </asset> | ||
+ | <library_nodes> | ||
+ | <node id="hello"/> | ||
+ | </library_nodes> | ||
+ | </COLLADA> | ||
+ | |||
+ | And the results of running the program. | ||
+ | |||
+ | node id: hello |
Revision as of 20:01, 9 April 2008
Be sure to read the section on creating documents first. It covers some important topics relevant to this section.
A simple example
Let's begin with a simple example of reading some information from a Collada document. We'll open the document and print the ID of the first <node> we find.
DAE dae; daeElement* root = dae.open("simpleImport.dae"); if (!root) { cout << "Document import failed.\n"; return 0; }
We create the DAE object then call DAE::open to open a file called "simpleImport.dae". If there is no file of that name in the current directory, or the file failed to open for some other reason, then the DAE::open method will return null. We check for that and print an error message if opening the document failed.
daeElement* node = root->getDescendant("node"); if (!node) cout << "No nodes found\n"; else cout << "node id: " << node->getAttribute("id") << endl;
Here we use the daeElement::getDescendant method to do a breadth-first search through the xml element tree for an element with the given name. This method will return null if it couldn't find an element with a matching name, which we check for. If it did find a matching element we use the daeElement::getAttribute method to print the value of the 'id' attribute.
The complete code.
#include <iostream> #include <dae.h> #include <dom/domCOLLADA.h> using namespace std; int main() { DAE dae; daeElement* root = dae.open("simpleImport.dae"); if (!root) { cout << "Document import failed.\n"; return 0; } daeElement* node = root->getDescendant("node"); if (!node) cout << "No nodes found\n"; else cout << "node id: " << node->getAttribute("id") << endl; return 0; }
The simpleImport.dae document.
<?xml version="1.0" encoding="UTF-8"?> <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1"> <asset> <contributor/> <created>2008-04-08T13:07:52-08:00</created> <modified>2008-04-08T13:07:52-08:00</modified> </asset> <library_nodes> <node id="hello"/> </library_nodes> </COLLADA>
And the results of running the program.
node id: hello