tinyxml读xml

main.cpp


#include <string>
#include <iostream>
#include <list>


using namespace std;



bool InitNode(TiXmlElement *xmlNode, APNode*& node)//why &
{
	// 1. 读取xmlNode的属性,类型等,明确到底是什么?
	if (xmlNode->Value() == NULL)
	{
		 return false;
	}

	// 2. 类型不同,处理方法不同
	if (strcmp("Part", xmlNode->Value()) == 0)
	{
		node = new PartNode();
		node->type = xmlNode->Value();
		((PartNode*)node)->path = xmlNode->Attribute("path");
		((PartNode*)node)->name = xmlNode->Attribute("name");

	}
	else if (strcmp("Assembly", xmlNode->Value()) == 0)
	{
		node = new AssemblyNode();
		node->type = xmlNode->Value();
		((AssemblyNode*)node)->count = xmlNode->Attribute("children_count");
		((AssemblyNode*)node)->name = xmlNode->Attribute("name");
	}
	else if (strcmp("scene_root", xmlNode->Value()) == 0)
	{
		node = new SceneNode();
		((SceneNode*)node)->type = xmlNode->Value();
		((SceneNode*)node)->name = xmlNode->Attribute("name");
		((SceneNode*)node)->description = xmlNode->Attribute("description");
		((SceneNode*)node)->count = xmlNode->Attribute("children_count");
	}
	return true;
	// 2.1 new一个AssemlyNode

	// 2.2 new一个PartNode

	// 3. 赋值	

	// 4. 如果需要,调用readstl
}




void BrowseChildren(TiXmlElement *parent, APNode*& headNode)
{
	TiXmlElement* child = parent->FirstChildElement();			// 得到第一个孩子节点
	while (child != NULL)							// 如果孩子节点不为空
	{
		APNode* pNode;
	     InitNode(child, pNode);
		 //static_cast<AssemblyNode*>(headNode)->m_pListChildren.push_back(pNode);
		 ((SceneNode*)headNode)->m_pListChildren.push_back(pNode);


		// 不能仅仅判断pNode是空,应该判断pNode的类型等信息
		if (pNode->type != "Part")
		{
			BrowseChildren(child, pNode);
		}
		child = child->NextSiblingElement();		// 找到下一个孩子节点
	}
}

bool ReadXML(char* path, APNode*& root)
{
	TiXmlDocument doc(path);							//打开XML文件
	if (!doc.LoadFile())											//检测打开是否成功
	{
		root = NULL;
		return	false;
	}

	TiXmlElement *xmlRoot = doc.RootElement();			//根元素
	//root = InitNode(xmlRoot, root);
	if (!InitNode(xmlRoot, root))
	{
		 root = NULL;
		 return true;
	}

	BrowseChildren(xmlRoot, root);
	//输出验证一下把东西装里面没?
	cout << static_cast<SceneNode*>(root)->m_pListChildren.size() << endl;;
   list<APNode*>::iterator it = static_cast<SceneNode*>(root)->m_pListChildren.begin();
   list<APNode*>::iterator itEnd = static_cast<SceneNode*>(root)->m_pListChildren.end();

   cout << "AssemblyNode size is " << static_cast<AssemblyNode*>(*it)->m_pListChildren.size() << endl;
  
   *it = static_cast<SceneNode*>(root)->m_pListChildren.back();
   cout << "name is " << static_cast<PartNode*>(*it)->name << endl
		   << "type is " << static_cast<PartNode*>(*it)->type << endl
		   << "path is " << static_cast<PartNode*>(*it)->path << endl;

	return true;
}



void main()
{


	APNode*  m_APNode = new APNode();

	ReadXML("example.xml", m_APNode);		// void ReadXML(string path, AssemblyNode* root)
	cout << static_cast<SceneNode*>(m_APNode)->m_pListChildren.size() << endl;

	//	list<APNode*>::iterator it = sence_Root->m_pListChildren.begin(); //有可能assemblynode 和partnode是并列的  

	//for (; it != sence_Root->m_pListChildren.end(); it++)
	//{
	//	
	//}
}

example.xml


<?xml version="1.0" encoding="utf-8"?>
<scene_root name = "Scene" children_count="3" description = "SimpleSence">
	<Assembly children_count="2" name="BuJian1">
		<Assembly children_count="3" name="BuJian1-1">
			<Assembly children_count="2" name="BuJian1-1-1">
				<Part path="data/a.stl" name="LingJian1">
				</Part>
				<Part path="data/b.stl" name="LingJian2">
				</Part>
			</Assembly>
			<Part path="data/c.stl" name="LingJian3">
			</Part>
			<Part path="data/a.stl" name="LingJian4">
			</Part>
		</Assembly>
		<Part path="data/b.stl" name="LingJian5">
		</Part>
	</Assembly>
	<Part path="data/c.stl" name="LingJian5">
	</Part>
	<Part path="data/c.stl" name="LingJian6">
	</Part>
</scene_root>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值