xml basic stuff

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
vegeta
Posts: 12
Joined: Fri May 12, 2006 4:56 pm
Location: the netherlands

xml basic stuff

Post by vegeta »

hey can someone tell me how i read the text between the <name>blabla</name> ? (xml :wink: )
becouse i tried the meshviewer tutorial but that only works if you have only one text to read but i need to read a lot more then that.
a different tutorial would also be nice

thanks in advance
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

According to the documentation, you want the EXN_TEXT element. The following was taken directly from irrXML.h...

Code: Select all

  <?xml version="1.0"?>
   <config>
      <!-- This is a config file for the mesh viewer -->
      <model file="dwarf.dea" />
      <messageText caption="Irrlicht Engine Mesh Viewer">
      Welcome to the Mesh Viewer of the "Irrlicht Engine".
      </messageText>
   </config>
The code for parsing this file would look like this:

Code: Select all

   #include <irrXML.h>
   using namespace irr; // irrXML is located in the namespace irr::io
   using namespace io;

   #include <string> // we use STL strings to store data in this example

   void main()
   {
      // create the reader using one of the factory functions

      IrrXMLReader* xml = createIrrXMLReader("config.xml");

      // strings for storing the data we want to get out of the file
      std::string modelFile;
      std::string messageText;
      std::string caption;

      // parse the file until end reached

      while(xml && xml->read())
      {
         switch(xml->getNodeType())
         {
         case EXN_TEXT:
            // in this xml file, the only text which occurs is the messageText
            messageText = xml->getNodeData();
            break;
         case EXN_ELEMENT:
            {
               if (!strcmp("model", xml->getNodeName()))
                  modelFile = xml->getAttributeValue("file");
               else
               if (!strcmp("messageText", xml->getNodeName()))
                  caption = xml->getAttributeValue("caption");
            }
            break;
         }
      }

      // delete the xml parser after usage
      delete xml;
   }
vegeta
Posts: 12
Joined: Fri May 12, 2006 4:56 pm
Location: the netherlands

Post by vegeta »

myes but like i said and also the tutorial said you can only do that if theres only one piece of text you need instead of having more elements with text. if i try the same thing with more text elements, i can only read the last element.

i tried: blabla = xml->getNodeData(name of element);
but doesn't work and i also don't need a case
Phxx
Posts: 13
Joined: Wed May 10, 2006 7:55 pm

Post by Phxx »

You need to keep track of the current Xml Node you are processing.

case io::EXN_ELEMENT you will check which kind of your text's it is

Code: Select all

if (!strcmp("nodename1", xml->getNodeName())) 
{
 trackingNode = 1;
}
else
{
 trackingNode = 2;
}
and in case EXN_TEXT you check which node you are processing as well

Code: Select all

switch(trackingNode)
{
case 1:
//do something with text
break;
case 2:
//do something else with text
break;

}
This is not the most elegant way, but it will get you started
vegeta
Posts: 12
Joined: Fri May 12, 2006 4:56 pm
Location: the netherlands

Post by vegeta »

YEEEEAAAAAH IT WORKS!!! thank you verry mutch man
vegeta
Posts: 12
Joined: Fri May 12, 2006 4:56 pm
Location: the netherlands

Post by vegeta »

hey i just found a shorter way
IXMLReader* xml = device->getFileSystem()->createXMLReader("config.xml");

while(xml && xml->read())
{
switch(xml->getNodeType())
{
case EXN_ELEMENT:

trackingNode = xml->getNodeName();

break;
case EXN_TEXT:
if (trackingNode == "udriver")//udriver is the xml element
{
driv = xml->getNodeData();
}
break;
}
}

if (xml)
xml->drop();
and don't get me wrong becouse without your help i wouldn't have figured this out
(this way just makes it easier becouse you don't have to define the xml elements every time)
Post Reply