After studying the loadScene() function in the CSceneManager.cpp and knowing that irrXML parses the XML file sequentially, I finally got it to work!
Here is my example code (only the important stuff):
<?xml version="1.0"?> <root> <config> <!-- This is a config file for the mesh viewer --> <models> <model file="dwarf.dea" /> <model file="dwarf1.dea" /> <model file="dwarf2.dea" /> </models> <messageText caption="Irrlicht Engine Mesh Viewer"> Welcome to the Mesh Viewer of the "Irrlicht Engine". </messageText> </config> <config> <!-- This is a config file for the mesh viewer --> <models> <model file="dwarf.dea" /> </models> <messageText caption="Irrlicht Engine Mesh Viewer"> Welcome to the Mesh Viewer of the "Irrlicht Engine". </messageText> </config> <config> <!-- This is a config file for the mesh viewer --> <models> <model file="dwarf.dea" /> </models> <messageText caption="Irrlicht Engine Mesh Viewer"> Welcome to the Mesh Viewer of the "Irrlicht Engine". </messageText> </config> </root>
typedef std::map<int, Config*> ConfigMap; ConfigMap configs;
void readXML() {
irr::io::IrrXMLReader* xml = irr::io::createIrrXMLReader("../data/data.xml"); int i = 0;
// parse the file until end reached while(xml && xml->read()) { bool endreached = false; switch(xml->getNodeType()) { case irr::io::EXN_ELEMENT_END: if (irr::core::stringw(L"root")==xml->getNodeName()) { endreached = true; } break; case irr::io::EXN_ELEMENT: if (irr::core::stringw(L"config")==xml->getNodeName()) { readConfig(xml, i++); } break; default: break; } if (endreached) break; } // delete the xml parser after usage delete xml;
}
void readConfig(irr::io::IrrXMLReader* xml, int i) { while(xml && xml->read()) { const irr::core::stringw name = xml->getNodeName();
switch (xml->getNodeType()) { case irr::io::EXN_ELEMENT_END: if (irr::core::stringw(L"config")==name) { return; } break; case irr::io::EXN_ELEMENT: if (irr::core::stringw(L"models")==name) { readModel(xml, i); } else if (irr::core::stringw(L"messageText")==name) { configs[i]->setMessageTextCaption(xml->getAttributeValue("caption")); configs[i]->setMessageText(xml->getNodeData());
} else { std::cout << "Found unknown element in xml file" << irr::core::stringc(xml->getNodeName()).c_str() << std::endl; } break; default: break; } } } void readModel(irr::io::IrrXMLReader* xml, int i) {
int s = 0;
while(xml && xml->read()) { switch (xml->getNodeType()) { case irr::io::EXN_ELEMENT_END: if (irr::core::stringw(L"models")==xml->getNodeName()) { return; } break; case irr::io::EXN_ELEMENT: if (irr::core::stringw(L"model")==xml->getNodeName()) { configs[i]->setModels(xml->getAttributeValue("file"), s++); } break; default: break; } } }
|