h

//2017的版本  Michael Wang 20180323
#ifdef  SceneHOld2017_Michael#include "tinystr.h"
#include "tinyxml.h"#include <atlconv.h>//wchar_t#include "Modeling\ModelingData.h"// 相关字符串的长度
#define LEN_NAME       32
#define LEN_DES           256enum NodeType{ NODE_BASE = 200, NODE_SCENE, NODE_ASSEMBLY, NODE_PART };class APNode
{
private:NodeType        nodeType;       // 节点类型//wchar_t            name[LEN_NAME];     // 节点名称__int32          counts;         // length of the byte array representation of the bojbectID                 id;char                 name[LEN_NAME];public:NodeType type() { return nodeType; }inline int base_byte_size();inline virtual int byte_size() { return base_byte_size(); }//void SetName(wchar_t _name[]){ ; };void SetName(const char* _name){ strcpy(name, _name); }inline virtual void ToCharArray(char buf[], int index);APNode(NodeType type) : id(-1), nodeType(type), counts(0) {};APNode() : nodeType(NODE_BASE), counts(0) {};APNode(char buf[], int index);virtual ~APNode();
};class PartNode : public APNode
{
private:__int32         m_nID;          // 零件ID,用于各个客户端和服务器之间通信ModelMesh     m_modelMesh;    // PlanApublic:ModelMesh& GetMesh() { return m_modelMesh; }inline int byte_size();virtual void ToCharArray(char buf[], int index);bool InitNode(TiXmlElement *xmlNode);PartNode() : APNode(NODE_PART) { };PartNode(char buf[], int index);virtual ~PartNode();
};class AssemblyNode : public APNode
{
private:list<APNode*> m_pListChildren;public:inline int byte_size();virtual void ToCharArray(char buf[], int index);bool InitNode(TiXmlElement *xmlNode);void BrowseChildren(TiXmlElement* xmlParent, AssemblyNode* nodeParent);const list<APNode*>& Children() { return m_pListChildren; }void AppendChild(APNode* pNode) { m_pListChildren.push_back(pNode); }AssemblyNode() : APNode(NODE_ASSEMBLY) { };AssemblyNode(NodeType type) : APNode(type) { };AssemblyNode(char buf[], int index);virtual ~AssemblyNode();
};class SceneNode : public AssemblyNode
{
private:int             product_id ;            // 不急着加进去//wchar_t      description[LEN_DES];char    description[LEN_DES];bool InitNode(TiXmlElement *xmlNode);public:inline int byte_size();virtual void ToCharArray(char buf[], int index);void SetDescription(const char* _description){ strcpy(description, _description); }bool ReadXML(const char* path);SceneNode(char buf[], int index);SceneNode() : AssemblyNode(NODE_SCENE) { product_id = 0; };virtual ~SceneNode();
};#endif /*SceneHOld2017_Michael*/

cpp

//
/2017的版本  Michael Wang 20180323
#ifdef  SceneCppOld2017_Michael
// **************************************************************************************************************************
// class APNode
// **************************************************************************************************************************
APNode::APNode(char buf[], int index)
{// get value of nodetypeint pos = index, len = sizeof(NodeType);memcpy_s(&nodeType, len, &buf[pos], len);pos += len;// get value of countlen = sizeof(__int32);memcpy_s(&counts, len, &buf[pos], len);pos += len;// get value of typelen = sizeof(ID);memcpy_s(&id, len, &buf[pos], len);pos += len;// get value of namelen = sizeof(char) * LEN_NAME;memcpy_s(&name, len, &buf[pos], len);}APNode::~APNode()
{
};void APNode::ToCharArray(char buf[], int index)
{// nodeTypeint pos = index, len = sizeof(NodeType);memcpy_s(&(buf[pos]), len, &nodeType, len);pos += len;// copy byte count of the objectint count = byte_size();len = sizeof(__int32);memcpy_s(&(buf[pos]), len, &count, len);pos += len;copy id to buf len = sizeof(ID);memcpy_s(&(buf[pos]), len, &id, len);pos += len;//int node = 0;//memcpy_s(&node, sizeof(__int32), &buf[44], sizeof(__int32));//namelen = sizeof(char) * LEN_NAME;memcpy_s(&(buf[pos]), len, &name, len);}int APNode::base_byte_size()
{return sizeof(__int32)*2 + sizeof(char)* LEN_NAME + sizeof(__int32);
}// **************************************************************************************************************************
// class PartNode
// **************************************************************************************************************************
PartNode::PartNode(char buf[], int index) : APNode(buf, index)
{//************************************int pos = index + base_byte_size();                    // 找到父类数据的结束位置int len = sizeof(__int32);memcpy_s(&m_nID, len, &buf[pos], len);             // 拷贝零件IDpos += len;m_modelMesh = ModelMesh(buf, pos);        //m_modelMeshpos += m_modelMesh.byte_size();}PartNode::~PartNode()
{
};int PartNode::byte_size()
{int len = base_byte_size();len += sizeof(__int32);                              //m_nIDlen += m_modelMesh.byte_size();                //m_modelMeshreturn len;
}void PartNode::ToCharArray(char buf[], int index)
{APNode::ToCharArray(buf, index);int pos = index + base_byte_size();int len = sizeof(__int32);memcpy_s(&(buf[pos]), len, &m_nID, len);pos += len;m_modelMesh.ToCharArray(buf, pos);len = m_modelMesh.byte_size();// wrong Michael Wang 20171204//int node = 0;//memcpy_s(&node, len, &buf[88], len);
}bool PartNode::InitNode(TiXmlElement *xmlNode)
{if (strcmp("Part", xmlNode->Value()) == 0){SetName(xmlNode->Attribute("name"));xmlNode->QueryIntAttribute("ID", &m_nID);//读取stl模型数据string partPaths = xmlNode->Attribute("path");const char* partChar = partPaths.c_str();if (strcmp("", partChar) != 0){// string 和wchar_t的转化std::wstring widstr = std::wstring(partPaths.begin(), partPaths.end());wchar_t* pwidstr = const_cast<wchar_t*>(widstr.c_str());//USES_CONVERSION;//const  char* names = "hello";//const WCHAR* cLineChar = A2W(names);GetMesh().ReadSTL(pwidstr);}return true;}return false;
}// **************************************************************************************************************************
// class AssemblyNode
// **************************************************************************************************************************
AssemblyNode::AssemblyNode(char buf[], int index) : APNode(buf, index)
{int pos = index + base_byte_size();                  // 找到父类数据的结束位置int len = sizeof(__int32);int count;                                                                 // 子节点的数量memcpy_s(&count, len, &buf[pos], len);             // 拷贝子节点pos += len;for (int i = 0; i < count; i++){NodeType type;                                                  // 子节点的类型memcpy_s(&type, sizeof(NodeType), &buf[pos], sizeof(NodeType));             // 拷贝子节点类型pos += sizeof(NodeType);switch (type){case NODE_ASSEMBLY:{AssemblyNode *assembly = new AssemblyNode(buf, pos);pos += assembly->byte_size();//delete assembly;break;}case NODE_PART:{PartNode *part = new PartNode(buf, pos);pos += part->byte_size();//delete part;break;}default:break;}}
}AssemblyNode::~AssemblyNode()
{for (list<APNode*>::iterator it = m_pListChildren.begin(); it != m_pListChildren.end(); it++)delete (*it);m_pListChildren.clear();
};int AssemblyNode::byte_size()
{int len = base_byte_size();for (list<APNode*>::iterator it = m_pListChildren.begin(); it != m_pListChildren.end(); it++)len += (*it)->byte_size();return len;
}void AssemblyNode::ToCharArray(char buf[], int index)
{APNode::ToCharArray(buf, index);           // 转换父类的数据int pos = index + base_byte_size();         // 转换完父类后的位置int len = sizeof(__int32);                 // 增加一个整形数,表示子节点的数量int count = (int)m_pListChildren.size();         // 子节点的数量memcpy_s(&buf[pos], len, &count, len);     // copy count到bufpos += len;                                  // 位置发生偏移// 遍历子节点,copy子节点数据到buffor (list<APNode*>::iterator it = m_pListChildren.begin(); it != m_pListChildren.end(); it++){(*it)->ToCharArray(buf, pos);pos += (*it)->byte_size();}int node = 0;memcpy_s(&node, sizeof(__int32), &buf[44], sizeof(__int32));//memcpy_s(&node, sizeof(__int32), &buf[48], sizeof(__int32));
}bool AssemblyNode::InitNode(TiXmlElement *xmlNode)
{if (strcmp("Assembly", xmlNode->Value()) == 0){//node->SetName(xmlNode->Attribute("name"));SetName(xmlNode->Attribute("name"));return true;}return false;
}void AssemblyNode::BrowseChildren(TiXmlElement* xmlParent, AssemblyNode* nodeParent)
{TiXmlElement* child = xmlParent->FirstChildElement();          // 得到第一个孩子节点while (child != NULL)                                      // 如果孩子节点不为空{if (strcmp("Part", child->Value()) == 0){PartNode* part = new PartNode();part->InitNode(child);nodeParent->AppendChild(part);}else if (strcmp("Assembly", child->Value()) == 0){AssemblyNode* assembley = new AssemblyNode();assembley->InitNode(child);nodeParent->AppendChild(assembley);BrowseChildren(child, assembley);}child = child->NextSiblingElement();                  // 找到下一个孩子节点}
}// **************************************************************************************************************************
// class AssemblyNode
// **************************************************************************************************************************
SceneNode::SceneNode(char buf[], int index) : AssemblyNode(buf, index)
{int pos = index + base_byte_size();int len = sizeof(__int32);memcpy_s(&buf[pos], len, &product_id, len);pos += len;//len = sizeof(wchar_t) * LEN_DES;len = sizeof(char) * LEN_DES;memcpy_s(&description, len, &buf[pos], len);}SceneNode::~SceneNode()
{
};int SceneNode::byte_size()
{//int len = base_byte_size();int len = AssemblyNode::byte_size();len += sizeof(__int32);//len += sizeof(wchar_t) * LEN_DES;len += sizeof(char) * LEN_DES;return len;
}void SceneNode::ToCharArray(char buf[], int index)
{AssemblyNode::ToCharArray(buf, index);//int pos = index + base_byte_size();int pos = index + AssemblyNode::byte_size();memcpy_s(&buf[pos], sizeof(__int32), &product_id, sizeof(__int32));pos += sizeof(__int32);//int len = sizeof(wchar_t) * LEN_DES;int len = sizeof(char) * LEN_DES;memcpy_s(&buf[pos], len, &description, len);}bool SceneNode::InitNode(TiXmlElement* xmlNode)
{if (strcmp("scene_root", xmlNode->Value()) == 0){//scene->SetName(xmlNode->Attribute("name"));//scene->SetDescription(xmlNode->Attribute("description"));SetDescription(xmlNode->Attribute("description"));xmlNode->QueryIntAttribute("productID", &product_id);return true;}return false;
}bool SceneNode::ReadXML(const char* path)
{TiXmlDocument doc(path);                                           //打开XML文件if (!doc.LoadFile())                                               //检测打开是否成功return    false;TiXmlElement* xmlRoot = doc.RootElement();                           //根元素if ( NULL == InitNode(xmlRoot) )return false;BrowseChildren(xmlRoot, this);return true;
}#endif  /*SceneCppOld2017_Michael*/

xml

<?xml version="1.0" encoding="utf-8"?>
<scene_root children_count="3" name = "Sence" description = "SimpleScene" productID = 00><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" ID = 01></Part><Part path="data/b.stl" name="LingJian2" ID = 2></Part></Assembly><Part path="data/c.stl" name="LingJian3" ID = 3></Part><Part path="data/a.stl" name="LingJian4" ID = 4></Part></Assembly><Part path="data/b.stl" name="LingJian5" ID = 5></Part></Assembly><Part path="data/c.stl" name="LingJian6" ID = 6></Part><Part path="data/d.stl" name="LingJian7" ID = 7></Part>
</scene_root>

C++读xml 文件信息相关推荐

  1. 使用tinyxml读xml文件信息到结构体

    下载TinyXML的网址:http://www.grinninglizard.com/tinyxml/ 使用TinyXML只需要将其中的6个文件拷贝到项目中就可以直接使用了,这六个文件是:tinyxm ...

  2. 02_Android写xml文件和读xml文件

     新建Android项目 编写AndroidManifest.xml,使本Android项目具有单元测试功能和写外设的权限. <?xml version="1.0" en ...

  3. python读xml文件生成头文件_python如何读取生成voc xml格式标注信息

    python生成voc xml文件 from lxml import etree class GEN_Annotations: def __init__(self, filename): self.r ...

  4. java读xml文件一般用什么_java读xml文件

    /** * 读取sms.xml配置文件信息 * @param file * @throws IOException * @throws JDOMException */ public ReadSMSF ...

  5. Qt: QXMLStreamReader,读XML文件实例

    主要接口: 1.TokenType QXmlStreamReader::readNext() 功能:读取下一个标记,并返回其类型. 主要的类型有: enum QXmlStreamReader::Tok ...

  6. TinyXML-2 读 XML 文件

    要读的XML文件 示例代码 #include <iostream> #include "tinyxml2.h"using namespace std; using na ...

  7. C# XPath 读取HL7-V3消息格式 xml 文件信息

    HL7 卫生信息交换标准(Health Level 7) 标准化的卫生信息传输协议,是医疗领域不同应用之间电子传输的协议.HL7汇集了不同厂商用来设计应用软件之间接口的标准格式,它将允许各个医疗机构在 ...

  8. python读xml文件生成头文件_Python根据指定文件生成XML的方法

    因项目需要根据指定格式的文件生成XML标注文件,可以方便使用LabelImg打开进行编辑和查看.其原始文件默认使用逗号进行分隔,如下所示: 第1个值:原始图片中切图小文件,以AIpng_x,其中x代表 ...

  9. python读xml文件生成.h头文件_PYTHON读写xml文件的方法

    要生成的xml文件格式如下: [python] sample xml thing ma xiaoju Springs Widgets, Inc. First I think widgets are g ...

  10. python读xml文件生成.h头文件_Python创建xml文件示例

    Python创建xml文件示例 这里有新鲜出炉的 Python 入门,程序狗速度看过来! Python 编程语言 Python 是一种面向对象.解释型计算机程序设计语言,由 Guido van Ros ...

最新文章

  1. 怎么快速插入 100 条数据,用时最短
  2. python代码实现二叉树的镜像树
  3. 伤疤好了有黑印怎么办_搞笑gif动态图片:“发现相亲对象的闺蜜更有实力,我后悔了”哈哈哈好一个见色起意...
  4. CTFshow php特性 web149
  5. python @staticmethod和@classmethod的作用
  6. StatisticalOutlierRemoval:离群点移除
  7. css3中clip属性
  8. ora-04021 无法锁表的解决办法
  9. C#:invoke 与 BeginInvoke使用区别
  10. AUTOSAR从入门到精通100讲(三十六)-AUTOSAR 通信服务两步走-CanSM概念-配置及代码分析
  11. Apple着手抛弃32位macOS应用程序
  12. 最详细的企业级可视化大屏教程,90%的需求看这一篇就足够了
  13. 天津医科大学计算机科学与技术,天津医科大学生物医学工程学院
  14. ASP.NET MVC - 使用Post, Redirect, Get (PRG)模式
  15. 【自动驾驶定位要求论文翻译】Localization Requirements for Autonomous Vehicles
  16. OpenJudge NOI 2.1 1813:熄灯问题
  17. allure趋势图无数据
  18. OFD格式文如何打开,可以转成PDF吗?
  19. Jenkins系列之——前言 Jenkins初识
  20. vba控制图表,excel图表,一键完成

热门文章

  1. linux6磁盘绑定,关于Linux 6使用udev绑定共享磁盘的测试
  2. mysql分组去掉重复记录_MYSQL中GROUP分组去除重复数据
  3. 字符串中索引位置是什么意思_女孩子左手中指戴戒指什么意思 不同位置各有不同...
  4. linux导入多个文件到myql
  5. 通过Maven找java source源码方法
  6. DBA和开发同事的一些代沟(一)
  7. 一个简单的文本编译器
  8. Ubuntu下安装配置Phabricator
  9. HDU 2258 Continuous Same Game
  10. Flash竖向大焦点图代码_网页代码站(www.webdm.cn)