ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > irrXML
forum topic indicator Parsing complex XML files
person icon
h.a.n.d
Guest
Quote
2009-02-13 14:23:39

Hi,

I'm recently using irrXML with my Irrlich projects and have some difficulties parsing an XML file with more than one of the same element:

i.e.

<?xml version="1.0"?>
<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>



Getting all config-elements is not that difficult, but how do I get i.e. all model-elements from the first config-element or all model-elements from all config-elements?

I've already used tinyXML for parsing XML and with that lib you can access directly all child elements of an parent element.
(i.e. give me all model(s) of the first config)

How can i accomplish that with the irrXML lib?

Thank you for your help!

person icon
niko
Moderator
Quote
2009-02-13 18:11:49

You simply would need to programmatically count the elements you are going through. Not as simple as with a DOM based parser as tinyxml, but possible. :)

person icon
h.a.n.d
Guest
Quote
2009-02-14 21:13:50

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;
}
}
}



Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |