c++读取xml文件

bool Prepare::Initial(string xmlPath)
{

	TiXmlDocument* Document = new TiXmlDocument();
	if (!Document->LoadFile(xmlPath.c_str(), TIXML_ENCODING_UNKNOWN))
	{
		cout << "无法加载xml文件!" << endl;
		cin.get();
		return false;
	}
	TiXmlElement* RootElement = Document->RootElement();		//根节点
	TiXmlAttribute *FirstAtt = RootElement->FirstAttribute();
	projectName = FirstAtt->Value();//读取工程名
	FirstAtt = FirstAtt->Next(); //下一个属性
	worker = FirstAtt->Value();//读取工作人员
	FirstAtt = FirstAtt->Next(); //下一个属性
	date = FirstAtt->Value();//读取工作时间
	FirstAtt = FirstAtt->Next(); //下一个属性
	simulationType = FirstAtt->Value();//读取分析类型
	TiXmlElement* NextElement = RootElement->FirstChildElement();		//根节点下的第一个节点层
	while (NextElement != NULL)		//判断有没有读完
	{
		if (NextElement->ValueTStr() == "Workbench")		//读到Workbench节点
		{
			TiXmlElement* ModelElement = NextElement->FirstChildElement();
			if (ModelElement->ValueTStr() =="Model")//读取Model节点
			{
				TiXmlElement* ModeFormat = ModelElement->FirstChildElement();
				modelFormat = ModeFormat->GetText();
				TiXmlElement* ModePath = ModeFormat->NextSiblingElement();
				modelPath = ModePath->GetText();
			}
			ModelElement = ModelElement->NextSiblingElement();
			if (ModelElement->ValueTStr() == "Material")
			{
	           TiXmlElement* MaterialPath = ModelElement->FirstChildElement();
			   materialInfoPath = MaterialPath->GetText();
			}			
		}
		NextElement = NextElement->NextSiblingElement();
		if (NextElement->ValueTStr() == "MultypleSys")
		{
			TiXmlElement* NameSelection = NextElement->FirstChildElement();
			while (NameSelection != NULL)
			{						
				if (NameSelection->ValueTStr() == "NameSelection")//读取Nameselection的内容
				{					
					TiXmlElement* SelectionInfo = NameSelection->FirstChildElement();					
					while (SelectionInfo!=NULL)
					{
						TiXmlAttribute *FirstAtt = SelectionInfo->FirstAttribute();
						nameSel_struct.selId = FirstAtt->Value();//Id
						FirstAtt = FirstAtt->Next(); //下一个属性
						nameSels_struct.nameSel_name = FirstAtt->Value();//读取名称	
						TiXmlElement* subSelectionInfo = SelectionInfo->FirstChildElement();
						while (subSelectionInfo != NULL)
						{
							TiXmlAttribute *FirstAtt = subSelectionInfo->FirstAttribute();
							nameSel_struct.selPartId = FirstAtt->Value();//selpartid
							FirstAtt = FirstAtt->Next(); //下一个属性
							nameSel_struct.nameSelNo = FirstAtt->Value();
							FirstAtt = FirstAtt->Next(); //下一个属性
							nameSel_struct.partName = FirstAtt->Value();
							nameSels_struct.singleNameSelections.push_back(nameSel_struct);//将这个结构体存入vector中
							nameSel_struct.reset();
							subSelectionInfo = subSelectionInfo->NextSiblingElement();
						}
						NameSelectionInfos.push_back(nameSels_struct);
						nameSels_struct.reset();									
						SelectionInfo = SelectionInfo->NextSiblingElement();
					}
				}

			  NameSelection = NameSelection->NextSiblingElement();
			  if (NameSelection->ValueTStr() == "MaterailSet")//读取材料设置节点
			  {
				  TiXmlElement* MaterialExcel = NameSelection->FirstChildElement();				 
				  materialReflectExcelPath = MaterialExcel->GetText();
			  }
			  NameSelection = NameSelection->NextSiblingElement();
			  if (NameSelection->ValueTStr() == "SimulationEnvironment")//读取材料设置节点
			  {
				TiXmlElement* SimulitinType = NameSelection->FirstChildElement();
				  while (SimulitinType!=NULL)
				  {
					  FirstAtt = SimulitinType->FirstAttribute();
					  singlgeAns_struct.analysisName = FirstAtt->Value();//获取当前分析名称
					  TiXmlElement* boundaryElement = SimulitinType->FirstChildElement();
					  TiXmlElement* condition = boundaryElement->FirstChildElement();
					  if (boundaryElement->ValueTStr() == "BoundaryCondition")//边界条件信息
					  {
						  while (condition!=NULL)
						  {                     
							    FirstAtt = condition->FirstAttribute();
						      restrain_struct.restrainId = FirstAtt->Value();//获取约束ID
							    TiXmlElement* conditionPara = condition->FirstChildElement();
							  if (conditionPara->ValueTStr() == "ConditionType")//获取约束类型信息
							  {
								  FirstAtt = conditionPara->FirstAttribute();
								  restrain_struct.restrain_type = FirstAtt->Value();//获取约束ID								
								  FirstAtt = FirstAtt->Next();
								  restrain_struct.value = FirstAtt->Value();								 														
							  }							
							  conditionPara = conditionPara->NextSiblingElement();
							  if (conditionPara->ValueTStr() == "RelateBody")//获取几何体信息
							  {
								 FirstAtt = conditionPara->FirstAttribute();
								 restrain_struct.geoInfo.body_No = FirstAtt->Value();//获取体编号
								 FirstAtt = FirstAtt->Next();
								 restrain_struct.geoInfo.body_name = FirstAtt->Value();//获取体名称
							  }							 
							  conditionPara = conditionPara->NextSiblingElement();
							  if (conditionPara->ValueTStr() == "GeoType")//获取几何体信息
							  {
								  FirstAtt = conditionPara->FirstAttribute();
								  restrain_struct.geoInfo.element_type = FirstAtt->Value();//获取体编号
								  FirstAtt = FirstAtt->Next();
								  restrain_struct.geoInfo.units = FirstAtt->Value();//获取体名称
							  }							 
							  conditionPara = conditionPara->NextSiblingElement();
							  if (conditionPara->ValueTStr() == "ElementCoordinate")//获取几何体信息
							  {
								  FirstAtt = conditionPara->FirstAttribute();
								  restrain_struct.geoInfo.coordinates = FirstAtt->Value();//获取							
							  }							
							  singlgeAns_struct.vector_restrain.push_back(restrain_struct);
							  restrain_struct.reset();							
							  condition = condition->NextSiblingElement();
						  }												  
					  }					
					  boundaryElement = boundaryElement->NextSiblingElement();
					  TiXmlElement* load = boundaryElement->FirstChildElement();
					  if (boundaryElement->ValueTStr() == "LoadConfigure")//载荷信息
					  {
						  while (load != NULL)
						  {
							  FirstAtt = load->FirstAttribute();
							  load_struct.loadId = FirstAtt->Value();
							  TiXmlElement* loadPara = load->FirstChildElement();
							  if (loadPara->ValueTStr() == "LoadType")
							  {
								  FirstAtt = loadPara->FirstAttribute();
								  load_struct.load_type = FirstAtt->Value();
								  FirstAtt = FirstAtt->Next();
								  load_struct.direction = FirstAtt->Value();
								  FirstAtt = FirstAtt->Next();
								  load_struct.value = FirstAtt->Value();	//如果是随机振动和响应谱分析此处参数为对应谱的路径参数							
							  }							
							  loadPara = loadPara->NextSiblingElement();
							  if (loadPara->ValueTStr() == "RelateBody")//获取几何体信息
							  {
								  FirstAtt = loadPara->FirstAttribute();    
								  load_struct.geoInfo.body_No = FirstAtt->Value();//获取体编号
								  FirstAtt = FirstAtt->Next();
								  load_struct.geoInfo.body_name = FirstAtt->Value();
							  }							
							  loadPara = loadPara->NextSiblingElement();
							  if (loadPara->ValueTStr() == "GeoType")//获取几何体信息
							  {
								  FirstAtt = loadPara->FirstAttribute();
								  load_struct.geoInfo.element_type = FirstAtt->Value();//获取体编号
								  FirstAtt = FirstAtt->Next();
								  load_struct.geoInfo.units = FirstAtt->Value();
							  }						
							  loadPara = loadPara->NextSiblingElement();
							  if (loadPara->ValueTStr() == "ElementCoordinate")//获取几何体信息
							  {
								  FirstAtt = loadPara->FirstAttribute();
								  load_struct.geoInfo.coordinates = FirstAtt->Value();//获取							
							  }							
							  singlgeAns_struct.vector_load.push_back(load_struct);
							  load_struct.reset();							 
							  load = load->NextSiblingElement();
						  }
					  }					
					  boundaryElement = boundaryElement->NextSiblingElement();
					  TiXmlElement* post = boundaryElement->FirstChildElement();
					  if (boundaryElement->ValueTStr() == "PostConfigure")//后处理信息
			         {
						  while (post != NULL)
						  {
							  FirstAtt = post->FirstAttribute();
							  post_struct.postId = FirstAtt->Value();
							  TiXmlElement* postPara = post->FirstChildElement();
							  if (postPara->ValueTStr() == "ResultType")
							  {
								  TiXmlAttribute *FirstAtt = postPara->FirstAttribute();
								  post_struct.result_type = FirstAtt->Value();								 						
							  }						
							  postPara = postPara->NextSiblingElement();
							  if (postPara->ValueTStr() == "RelateBody")//获取几何体信息
							  {
								  FirstAtt = postPara->FirstAttribute();
								  post_struct.geoInfo.body_No = FirstAtt->Value();//获取体编号
								  FirstAtt = FirstAtt->Next();
								  post_struct.geoInfo.body_name = FirstAtt->Value();
							  }							
							  postPara = postPara->NextSiblingElement();
							  if (postPara->ValueTStr() == "GeoType")//获取几何体信息
							  {
								  FirstAtt = postPara->FirstAttribute();
								  post_struct.geoInfo.element_type = FirstAtt->Value();//获取体编号
								  FirstAtt = FirstAtt->Next();
								  post_struct.geoInfo.units = FirstAtt->Value();
							  }							
							  postPara = postPara->NextSiblingElement();
							  if (postPara->ValueTStr ()== "ElementCoordinate")//获取几何体信息
							  {
								  FirstAtt = postPara->FirstAttribute();
								  post_struct.geoInfo.coordinates = FirstAtt->Value();							
							  }						
							  singlgeAns_struct.vector_post.push_back(post_struct);
							  post_struct.reset();							
							  post = post->NextSiblingElement();
						  }
					  }					
					  vector_AllInfos.push_back(Pair(singlgeAns_struct.analysisName, singlgeAns_struct));
					  singlgeAns_struct.reset();					
					  SimulitinType = SimulitinType->NextSiblingElement();
				  }
				  NameSelection=NameSelection->NextSiblingElement();		
			  }
			}		
		}
		NextElement = NextElement->NextSiblingElement();		
	}
	return true;
}

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用第三方库来读取和解析XML文件,比如 `pugixml`。以下是使用 `pugixml` 库读取XML文件的示例代码: 首先,需要下载并安装 `pugixml` 库。可以从 https://pugixml.org/ 下载源代码,并将 `pugixml.hpp` 和 `pugixml.cpp` 文件添加到你的C++项目中。 接下来,可以使用以下示例代码来读取XML文件: ```cpp #include <iostream> #include "pugixml.hpp" int main() { pugi::xml_document doc; if (!doc.load_file("data.xml")) { std::cout << "Failed to load 'data.xml'" << std::endl; return 1; } // 获取根节点 pugi::xml_node root = doc.child("root"); // 遍历子节点 for (pugi::xml_node node = root.first_child(); node; node = node.next_sibling()) { std::cout << "Node name: " << node.name() << std::endl; // 获取节点属性 std::cout << "Attribute value: " << node.attribute("attribute_name").value() << std::endl; // 获取节点文本内容 std::cout << "Node value: " << node.child_value() << std::endl; // 获取子节点 pugi::xml_node childNode = node.child("child_node"); if (childNode) { // 处理子节点 } } return 0; } ``` 确保将 `data.xml` 替换为你要读取的实际XML文件的路径。使用 `load_file` 函数可以加载XML文件,然后可以使用 `child`、`attribute`、`child_value` 等函数来获取节点的名称、属性和文本内容。 请注意,上述示例假设你已经将 `pugixml.hpp` 和 `pugixml.cpp` 文件添加到你的项目中,并且已经正确配置了编译器和链接器,以便能够正确编译和运行代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值