1

I have an XML file :

<shape type="obj">
    <string name="filename" value="meshes/cbox_luminaire.obj"/>
    <transform name="toWorld">
        <translate x="0" y="-0.5" z="0"/>
    </transform>

    <ref id="light"/>

    <emitter type="area">
        <spectrum name="radiance" value="400:0, 500:8, 600:15.6, 700:18.4"/>
    </emitter>
</shape>
<shape type="obj">
    <string name="filename" value="meshes/cbox_back.obj"/>

    <ref id="white"/>
</shape>

I need to extract the file path of shapes.

    XMLElement * a = doc.FirstChildElement( "scene" );//->FirstChildElement("shape");

    for(XMLElement* elem = a->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
    {
        std::string elemName = elem->Value();
        if(elemName=="shape")
            toto.push_back(elem);
    }

  for(unsigned int i =0;i<toto.size();i++)
  {
    const XMLAttribute* tmp = toto[i]->FirstAttribute ();
    std::cout<<tmp->Name()<<":"<<tmp->Value()<<"\n";

  }

The only data i can retrive is the first attribute aka type:obj How to get the filename, tranformation data if it exists and other data?

bob87
  • 11
  • 2
  • XMLNode * son = doc.FirstChildElement("scene")->FirstChildElement("shape"); for(XMLNode * current=son;current!=NULL;current=current->NextSibling()) { readShape(current); } and void readShape(XMLNode *n) { if(strcmp(n->ToElement()->FirstAttribute()->Value(),"obj")==0) { const char* path = n->FirstChildElement()->FirstAttribute()->Next()->Value(); std::cout<FirstChild();current!=NULL;current=current->NextSibling()) { if(strcmp(current->Value(),"transform")==0) std::cout<<"transformation!"<<"\n"; } } } – bob87 Jan 08 '16 at 11:12

1 Answers1

0

This worked beautifully. Some minor corrections to your code above:

void readShape(XMLNode* n)
{
  if (strcmp(n->ToElement()->FirstAttribute()->Value(), "obj") == 0)
  {
    const char *path = n->FirstChildElement()->FirstAttribute()->Next()->Value();
    std::cout << path << "\n";
    for (XMLNode * current = n->FirstChild();
      current != NULL;
      current = current->NextSibling())
    {
      if (strcmp(current->Value(), "transform") == 0)
        std::cout << "transformation!" << "\n";
    }
  }
}

and in your main function:

tinyxml2::XMLDocument doc;
doc.LoadFile("Path/To/Xml");


XMLNode * son = doc.FirstChildElement("scene")->FirstChildElement("shape");

for (XMLNode * current = son; current != NULL; current = current->NextSibling())
{ 
  readShape(current); 
}

doc.Clear();