C++使用TinyXml实现读取XMl文件

  #include "ParseXML.h"

  ParseXML::ParseXML()

  {

  }

  ParseXML::~ParseXML()

  {

  }

  bool ParseXML::ReadParaXml(string m_strXmlPath, vector& vecNode)

  {

  Cookbook* pNode = new Cookbook();

  //读取xml文件中的参数值

  TiXmlDocument* Document = new TiXmlDocument();

  if (!Document->LoadFile(m_strXmlPath.c_str()))

  {

  cout << "无法加载xml文件!" << endl;

  cin.get();

  return false;

  }

  TiXmlElement* RootElement = Document->RootElement(); //根目录

  TiXmlElement* NextElement = RootElement; //根目录下的第一个节点层

  while (NextElement != NULL) //判断有没有读完

  {

  if (NextElement->ValueTStr() == "menu") //读到menu节点

  {

  TiXmlElement* BoxElement = NextElement->FirstChildElement();

  while (BoxElement->ValueTStr() != "name") //读到name节点

  {

  BoxElement = BoxElement->NextSiblingElement();

  }

  pNode->name = BoxElement->GetText();

  BoxElement = BoxElement->NextSiblingElement();

  while (BoxElement->ValueTStr() == "food") // 读到food节点

  {

  pNode->food.push_back(BoxElement->GetText());

  BoxElement = BoxElement->NextSiblingElement();

  }

  while (BoxElement->ValueTStr() == "spices") // 读到spices节点

  {

  pNode->spices.push_back(BoxElement->GetText());

  BoxElement = BoxElement->NextSiblingElement();

  }

  while (BoxElement != nullptr && BoxElement->ValueTStr() == "step")// 读到step节点

  {

  pNode->step.push_back(BoxElement->GetText());

  BoxElement = BoxElement->NextSiblingElement();

  }

  vecNode.push_back(*pNode);

  pNode->clear();

  }

  NextElement = NextElement->NextSiblingElement();

  }

  //释放内存

  delete pNode;

  delete Document;

  cout << "完成xml的读取" << endl;

  return true;

  }