1, Scene.h 文件

//#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*/

对应的Scene.cpp

//#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*/

main()主函数

void main()
{SceneNode* m_pScene = new SceneNode(); m_pScene->ReadXML("sample.xml");
}

Xml文件, sample.xml

<?xml version="1.0" encoding="utf-8"?>
<scene_root children_count = "3" name = "Sence" description = "SimpleScene" productID = 365><Assembly children_count = "3" name = "BuJian1-1"><Part path = "data/a.stl" name = "LingJian4" ID = 4></Part></Assembly><Part path="data/d.stl" name="lingjian7" ID = 7></Part>
</scene_root>

C#解析  SceneAnalyse.cs

using System;
using System.Collections;
using System.Collections.Generic;
using ServerCommunication;namespace SceneDataProcess
{class StringLength{public  const  int Len_Name  = 32;public  const int Len_Des    = 256; }public class APNode{public DataType NodeType;public Int32 Counts;public Int32 ID;public string Name;public virtual byte[] ToByteArray(){Counts = byte_size();byte[] _nodeType = BitConverter.GetBytes((Int32)NodeType);byte[] _counts = BitConverter.GetBytes(Counts);byte[] _id = BitConverter.GetBytes(ID);byte[] _name = System.Text.Encoding.Default.GetBytes(Name);byte[] ret = new byte[byte_size()];_nodeType.CopyTo(ret, 0);_counts.CopyTo(ret, _nodeType.Length );_id.CopyTo (ret, _nodeType.Length + _counts.Length);_name.CopyTo(ret,  _nodeType.Length + _counts.Length + _id.Length);return ret;}public virtual Int32 byte_size(){return sizeof(Int32)*2 + StringLength.Len_Name + sizeof(Int32);}public APNode(DataType NodeType){ID = -1;NodeType = DataType.NODE_BASE;Name = ""; Counts = 0;}public APNode(byte[] data, int index = 0){NodeType = (DataType)BitConverter.ToInt32(data, index);Counts = BitConverter.ToInt32(data, index + sizeof(DataType) );ID = BitConverter.ToInt32 (data, index + sizeof(DataType) + sizeof(Int32));Name = System.Text.Encoding.Default.GetString (data, index + sizeof(DataType) + sizeof(Int32)*2, StringLength.Len_Name);//Name = BitConverter.ToString(data, index + sizeof(DataType) + sizeof(Int32), StringLength.Len_Name);}};public class PartNode : APNode{public Int32 m_nID;public ModelMesh modelMesh = new ModelMesh();public override byte[] ToByteArray(){byte[] _base = base.ToByteArray();byte[] _m_nID = BitConverter.GetBytes((Int32)m_nID);//modelmeshbyte[] _modelMesh = modelMesh.ToByteArray();int len = _modelMesh.Length;byte[] l_p = BitConverter.GetBytes((Int32)len);byte[] ret = new byte[byte_size()];_base.CopyTo (ret, 0);_m_nID.CopyTo (ret, _base.Length);l_p.CopyTo(ret, _base.Length + _m_nID.Length );_modelMesh.CopyTo(ret,  _base.Length + _m_nID.Length + l_p.Length);return ret;}public override Int32 byte_size(){// base length and Path//int index = base.byte_size() + sizeof(Int32);int index = base.byte_size() + sizeof(Int32) + modelMesh.byte_size();return index;}public PartNode(DataType NodeType):base(NodeType){NodeType = DataType.NODE_PART;  Name = ""; }           public PartNode(byte[] data, int start = 0) : base(data, start){int index = start + base.byte_size();index += sizeof(Int32);// modelmesh -----------------------------------------------------modelMesh = new ModelMesh(data, index);index += modelMesh.byte_size();}};public class AssemblyNode : APNode{public List<APNode> linkListChildren = new List<APNode> (); //public LinkedList<APNode> linkListChildren = new LinkedList<APNode> ();//public ArrayList linkListChildren  = new ArrayList();public override byte[] ToByteArray(){byte[] _base = base.ToByteArray();byte[] ret = new byte[byte_size()];_base.CopyTo(ret, 0);int index = _base.Length;// linkListChildren -----------------------------------------------------int count = linkListChildren.Count;byte[] len = BitConverter.GetBytes((Int32)count);len.CopyTo(ret, index);index += len.Length;foreach (var item in linkListChildren) {byte[] _item = item.ToByteArray();_item.CopyTo(ret, index);index += _item.Length;//?????????????}return ret;}public override Int32 byte_size(){// base length and numint index = base.byte_size();// linkListChildren -----------------------------------------------------foreach (var item in linkListChildren) {index += item.byte_size();//?????????????}return index;}public AssemblyNode(DataType NodeType):base(NodeType){NodeType = DataType.NODE_ASSEMBLY;  Name = ""; }         public AssemblyNode(byte[] data, int start = 0) : base(data, start){int index = start + base.byte_size();// linkListChildren -----------------------------------------------------//count is productID  40int count = BitConverter.ToInt32(data, index);index += sizeof(Int32);for (int i = 0; i < count; i++){DataType NodeTypes;NodeTypes = (DataType)BitConverter.ToInt32(data, index);if (DataType.NODE_PART == NodeTypes) {PartNode pp = new PartNode (data, index);linkListChildren.Add(pp);index += pp.byte_size();}if (DataType.NODE_ASSEMBLY == NodeTypes) {AssemblyNode pa = new AssemblyNode (data, index);linkListChildren.Add (pa);index += pa.byte_size ();}}}};public class SceneNode : AssemblyNode{public Int32 ProductId;public string Description;public override byte[] ToByteArray(){byte[] _base = base.ToByteArray();byte[] _description = System.Text.Encoding.Default.GetBytes(Description);byte[] _productId = BitConverter.GetBytes (ProductId);byte[] ret = new byte[byte_size()];_base.CopyTo(ret, 0);_description.CopyTo(ret, _base.Length);_productId.CopyTo (ret, _base.Length + StringLength.Len_Des);return ret;}public override Int32 byte_size(){// base length and numint index = base.byte_size() + StringLength.Len_Des + sizeof(Int32);return index;}public SceneNode(DataType NodeType):base(NodeType){NodeType = DataType.NODE_SCENE;  Name = ""; Description = ""; ProductId = 0;}            public SceneNode(byte[] data, int start = 0) : base(data, start){int index = start + base.byte_size();ProductId = BitConverter.ToInt32 (data, index);index += sizeof(Int32);//Description = BitConverter.ToString (data, index, StringLength.Len_Des);Description = System.Text.Encoding.Default.GetString (data, index , StringLength.Len_Des);}};}

C++读xml文件, C#解析对应的文件相关推荐

  1. python读取本地文件-python解析本地HTML文件

    Python使用爬虫技术时,每运行一次,本地都会访问一次主机.为避免完成程序前调试时多次访问主机增加主机负荷,我们可以在编写程序前将网页源代码存在本地,调试时访问本地文件即可.现在我来分享一下爬取资料 ...

  2. 上传php文件不能解析,浅谈文件解析及上传漏洞

    中国菜刀 在web渗透中,我最期待两种漏洞,一种是任意命令执行漏洞,如struct2漏洞等:另一种是文件上传漏洞,因为这两种漏洞都是获取服务器权限最快最直接的方法.而对于任意命令执行漏洞,如果是通过内 ...

  3. 通过LNK文件(快捷方式)解析出目标文件的路径

    转载自:https://blog.csdn.net/yoie01/article/details/8688686 尼玛的~网上找了一堆资料都是有问题的代码,各种转发,错的东西传来传去,误人子弟!!! ...

  4. python 实现文件的批量压缩为.zip格式+.zip格式文件的解析

    python 实现文件的批量压缩为.zip格式+.zip格式文件的解析 python 实现文件的批量压缩为.zip格式 Python解析.zip文件的常见函数 python 实现文件的批量压缩为.zi ...

  5. Office文件的奥秘——.NET平台下不借助Office实现Word、Powerpoint等文件的解析(完)...

    原文 http://www.cnblogs.com/mayswind/archive/2013/04/01/2991271.html [题外话] 这是这个系列的最后一篇文章了,为了不让自己觉得少点什么 ...

  6. 教你如何在Python中读,写和解析CSV文

    摘要:在这篇文章中关于"在Python如何阅读CSV文件"中,我们将学习如何读,写和解析的CSV文件的Python. 您知道将表格数据存储到纯文本文件背后的机制是什么吗?答案是CS ...

  7. 一、STM32启动文件详细解析

    一.STM32启动文件详细解析 STM32启动文件详细解析(V3.5.0) 以:startup_stm32f10x_hd.s为例 [cpp] view plain copy ;************ ...

  8. java opencsv 乱码_教你如何在Python中读,写和解析CSV文

    摘要:在这篇文章中关于"在Python如何阅读CSV文件"中,我们将学习如何读,写和解析的CSV文件的Python. 您知道将表格数据存储到纯文本文件背后的机制是什么吗?答案是CS ...

  9. Office文件的解析

    Office文件的解析 引用:http://www.langye.com/a/2013417/237.shtml [题外话] 这是这个系列的最后一篇文章了,为了不让自己觉得少点什么,顺便让自己感觉完美 ...

  10. ECC有关DER文件的解析(Java)

    ECC有关DER文件的解析(Java) 本篇博客提供有关ECC的DER文件的Java解析方式,如ECC公钥和ECC签名值对应DER文件的解析,PEM文件也能够使用本博客中提供的解析方式进行解析,PEM ...

最新文章

  1. 地图上制作线路的动画_魔兽争霸重制版不只是表面上这么简单,新版编辑器制作地图更容易...
  2. Springboot项目中配置tomcta监控日志
  3. [IOI2011]Race
  4. 关于css浮动的一点思考
  5. php中include和require,在PHP中include和require到底有什么区别呢?
  6. android 获取手机运行的进程
  7. linux搭建虚拟化平台报告,部署KVM虚拟化平台------搭建(示例代码)
  8. linux常用架构,Linux常用到的一些命令-Go语言中文社区
  9. Java实例化后自动执行_Java的实例化顺序(程序执行顺序)
  10. 使用计算机教学的意义,信息技术在教学中的作用
  11. 【清单】语言、框架及库的官方文档、examples、tutorials
  12. sklearn 学习实践之——基于自带数据集(波士顿房价、鸢尾花、糖尿病等)构建分类、回归模型
  13. 最坑爹的硬盘:希捷ST2000DM001
  14. Win10(21h2)十一代i7,HUD750,打驱动一直重启
  15. Latex 提示错误Improper alphabetic constant
  16. 动态内存的开辟与释放
  17. 资料汇总更新|软件安装包、书籍、源码、技术文档、手册……
  18. Aspen Plus教程-孙兰义-例7.1-质量分数求解摩尔回收率
  19. C语言初学者需要知道的十句话,听说不知道的人都没学好编程
  20. spring boot and php

热门文章

  1. EasyUI配置当点击一个datagrid的checkbox选中或取消选中记录的时候刷新另一个datagrid数据的方法
  2. 从事计算机工作的应该,未来想从事计算机方面的工作,现在应该学习些什么东西?...
  3. java apns ssl错误_无法使用Javapns/Javaapns SSL握手失败发送推送通知
  4. exfat最佳单元大小_ICLR2019最佳论文!神经网络子网络压缩10倍,精确度还能保持不变...
  5. ubuntu 下c语言开发环境搭建,Ubuntu下Object-c的开发环境搭建
  6. java计算两点距离_Java 使用经度计算两点之间的距离?
  7. 实力封装:Unity打包AssetBundle(四)
  8. 【钛坦白】榛杏科技CEO周开宇:ICO和区块链的创新方向选择
  9. 火星上网不是梦,国际空间站开测星际互联网DTN服务
  10. Android:RGB颜色对照表