TinyXML 2数据提取

TinyXML 2数据提取,xml,tinyxml2,Xml,Tinyxml2,我有一个XML文件: <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"/> &l

我有一个XML文件:

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


我需要提取形状的文件路径

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

  }
XMLElement*a=doc.FirstChildElement(“场景”);//->第一子元素(“形状”);
对于(xmlement*elem=a->FirstChildElement();elem!=NULL;elem=elem->NextSiblingElement())
{
std::string elemName=elem->Value();
if(elemName==“shape”)
toto.推回(elem);
}
for(unsigned int i=0;ifirstatAttribute();

std::cout这工作非常出色。对上面的代码进行了一些小的修改:

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

这工作得很好。对上面的代码进行了一些小的修改:

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

XMLNode*son=doc.FirstChildElement(“场景”)->FirstChildElement(“形状”);for(XMLNode*current=son;current!=NULL;current=current->NextSibling(){readShape(current);}和void readShape(XMLNode){if(strcmp(n->ToElement()->FirstAttribute()->Value(),“obj”)=0){const char path=n->FirstChildElement()->FirstAttribute()->Value();std::coutXMLNode*son=doc.FirstChildElement(“场景”)->FirstChildElement(“形状”);for(XMLNode*current=son;current!=NULL;current=current->NextSibling(){readShape(current);}和void readShape(XMLNode){if(strcmp(n->ToElement()->FirstAttribute()->Value(),“obj”)=0){const char path=n->FirstChildElement()->FirstAttribute()->Next()->Value();std::cout