解析XML

解析iworld XML,拿到entity和VisibleVolume的数据

int ParseiWorlds::readXML(const bpath &dir)
{
ptree pt;
try
{
read_xml(dir.string(), pt);
}
catch (const std::exception& e)
{
cout << "read error" << e.what() << endl;
return -1;
} ptree ptchild = pt.get_child("World.Levels.Level.Root.Entity.RootArea.Root.Entity.Entities"); for (auto& data : ptchild)
{
entity Object; ptree ptchildEntity = data.second.get_child("Root.Entity"); cout << "Name: " << ptchildEntity.get<string>("Name") << endl;
cout << "Transform: " << ptchildEntity.get<string>("Transform") << endl;
Object.name = ptchildEntity.get<string>("Name");
transformMatrix(ptchildEntity.get<string>("Transform"), Object.transform); //cube等resource记录在PPrimitives下
auto PrimitivesCount = ptchildEntity.get<int>("Primitives.<xmlattr>.count");
if (PrimitivesCount == 1)
{
ptchildEntity = ptchildEntity.get_child("Primitives.Element.Root.Entity"); cout << "Resource: " << ptchildEntity.get<string>("Resource") << endl;
Object.resource = ptchildEntity.get<string>("Resource");
} //visbilityVolume
auto VisibilityCube = ptchildEntity.get_child_optional("VisibilityCube.Root.Entity.Resource");
if (VisibilityCube)
{
cout << "Resource: " << ptchildEntity.get<string>("VisibilityCube.Root.Entity.Resource") << endl;
Object.resource = ptchildEntity.get<string>("VisibilityCube.Root.Entity.Resource");
Object.isVisibilityVolume = true;
} m_objectMap[Object.name] = Object;
cout << endl;
}
cout << "Object cnt:" << m_objectMap.size() << endl;
return 1;
}

生成XML

写入XML时候,注意使用setting参数,保证文件内容样式有缩进有对齐

    boost::property_tree::xml_writer_settings<string> settings = boost::property_tree::xml_writer_make_settings<string>('\t', 1);

	//把property_tree 转为XML文件
write_xml(path, pt, std::locale(), settings);
void VisibilityVolume::WriteResourceXML(const string & path)
{
ptree pt;
ptree Resource, Version, SVisibilityCubeData, tNull; Resource.put<int>("Version", 1);
pt.add_child("Resource", Resource); ptree Entity, x_WorlBB, x_TileSize, x_SizeX, x_SizeY, x_SizeZ, x_MaxOccluID, x_EffectBitCount, x_EffectBytesPerTile, x_SampleCount; string world_Str="";
world_Str = "(" + std::to_string(minX) + ',' + std::to_string(minY) + ',' + std::to_string(minZ) + ',' +
std::to_string(maxX) + ',' + std::to_string(maxY) + ',' + std::to_string(maxZ) + ')'; x_WorlBB.put_value<string>(world_Str);
x_TileSize.put_value<int>(cellSize);
x_SizeY.put_value<int>(SizeX);
x_SizeY.put_value<int>(SizeY);
x_SizeZ.put_value<int>(SizeZ); x_MaxOccluID.put_value<int>(MaxOccluId);
x_EffectBitCount.put_value<int>(EffectBitCount);
x_EffectBytesPerTile.put_value<int>(EffectBytesPerTile);
x_SampleCount.put_value<int>(SampleCount); Entity.put("<xmlattr>.type", "SVisibilityCubeData");
Entity.add_child("WorlBB", x_WorlBB);
Entity.add_child("TileSize", x_TileSize);
Entity.add_child("SizeX", x_SizeY);
Entity.add_child("SizeY", x_SizeY);
Entity.add_child("SizeZ", x_SizeZ);
Entity.add_child("MaxOccluId", x_MaxOccluID);
Entity.add_child("EffectBitCount", x_EffectBitCount);
Entity.add_child("EffectBytesPerTile", x_EffectBytesPerTile);
Entity.add_child("SampleCount", x_SampleCount); SVisibilityCubeData.add_child("Root.Sub", tNull);
SVisibilityCubeData.add_child("Root.Entity", Entity); pt.add_child("Resource.SVisibilityCubeData", SVisibilityCubeData);
//设置写入xml文件的格式,
boost::property_tree::xml_writer_settings<string> settings = boost::property_tree::xml_writer_make_settings<string>('\t', 1); //把property_tree 转为XML文件
write_xml(path, pt, std::locale(), settings);
}
<?xml version="1.0" encoding="utf-8"?>
<Resource>
<Version>1</Version>
<SVisibilityCubeData>
<Root>
<Sub/>
<Entity type="SVisibilityCubeData">
<WorlBB>(-50.000000,-45.000000,-50.000000,50.000000,55.000000,50.000000)</WorlBB>
<TileSize>5</TileSize>
<SizeX>20</SizeX>
<SizeY>20</SizeY>
<SizeZ>20</SizeZ>
<MaxOccluId>17</MaxOccluId>
<EffectBitCount>17</EffectBitCount>
<EffectBytesPerTile>3</EffectBytesPerTile>
<SampleCount>141</SampleCount>
</Entity>
</Root>
</SVisibilityCubeData>
</Resource>

修改XML

先读取XML,获得ptree,修改ptree中的节点,注意读取的时候,read_xml参数使用trim_whitespace裁剪空格和换行

read_xml(m_iworldDir.string(), pt, boost::property_tree::xml_parser::trim_whitespace);
void ParseiWorlds::WriteOccluID(map<string, entity> &iworldObj)
{
ptree pt;
try
{
read_xml(m_iworldDir.string(), pt, boost::property_tree::xml_parser::trim_whitespace);
}
catch (const std::exception& e)
{
cout << "read error" << e.what() << endl;
} ptree &ptchild = pt.get_child("World.Levels.Level.Root.Entity.RootArea.Root.Entity.Entities"); //遍历所有items
for (auto& data : ptchild)
{
ptree &items = data.second.get_child("Root.Entity");
string itemName = items.get<string>("Name");
int occludID = iworldObj[itemName].OccluId; //cube resource等信息记录在Primitives下
auto PrimitivesCount = items.get<int>("Primitives.<xmlattr>.count");
if (PrimitivesCount == 1)
{
ptree &ptchildEntity = items.get_child("Primitives.Element.Root.Entity"); ptchildEntity.put<int>("OccluId", occludID);
}
} //设置写入xml文件的格式
boost::property_tree::xml_writer_settings<string> settings = boost::property_tree::xml_writer_make_settings<string>('\t', 1); //把property_tree 转为XML文件
write_xml(m_iworldDir.string(), pt, std::locale(), settings); }

BOOST 解析,修改,生成xml样例的更多相关文章

  1. maven中使用dom4j解析、生成XML的简易方法

    此片文章主要写一些关于如何在maven工程中使用dom4j来解析或生成XML的建议方法,实际可使用的写法不仅限于如下所写的样例代码.此处进攻快速入手和提供思路使用. 首先配置pom.xml中的依赖的包 ...

  2. SAX解析和生成XML文档

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui031 ...

  3. 使用Pull解析器生成XML文件和读取xml文件

    有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...

  4. 使用Pull解析器生成XML文件

    有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...

  5. java使用xsd校验xml样例

    知识点:XSD文件是指XML结构定义 ( XML Schemas Definition )文件,是DTD的替代品.可以用一个指定的XML Schema来验证某个XML文档,以检查该XML文档是否符合其 ...

  6. Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml) -- 样例(6)

    managed-schema 样例: <?xml version="1.0" encoding="UTF-8" ?> <!-- License ...

  7. python 解析与生成xml

    xml.etree.ElementTree模块为xml文件的提取和建立提供了简单有效的API.下文中使用ET来代表xml.etree.ElementTree模块. XML是一种内在的分层的数据形式,展 ...

  8. JavaWeb_使用dom4j解析、生成XML文件

    dom4j 官网 xml解析DOM文档对象模型(树形结构) DOM方式解析:把xml文档加载到内存形成树形结构,可以进行增删改的操作 Learn   使用dom4j解析文件"NewFile. ...

  9. java 解析并生成 XML

    在 java 中使用 Dom4j 解析 XML 对 XML 文件的解析,通常使用的是 Dom4j 和 jdom 作为XML解析工具. 在此只介绍下 Dom4j 对 XML 文件的解析使用方法. 1. ...

随机推荐

  1. 20个「MySQL」经典面试题,答对转dba 2w+「附答案」

    1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 2.MySQL中myisam与innodb的区别,至少5点 (1).问5点不同: (2).innodb引擎的4大特性 (3). ...

  2. Infix to Postfix Expression

    Example : Infix : (A+B) * (C-D) ) Postfix: AB+CD-* 算法: 1. Scan the infix expression from left to rig ...

  3. 浅析C语言中printf(),sprintf(),scanf(),sscanf()的用法和区别

    printf语法: #include <stdio.h>int printf( const char *format, ... ); printf()函数根据format(格式)给出的格式 ...

  4. Docker 运行 MYSQL 数据库的总结

    公司里面要求做一个小demo 学习java相关的东西 然后使用了mysql数据库 很早之前做过mysql的容器化运行. 现在想想已经忘记的差不多了  所以这里总结一下 docker化运行mysql数据 ...

  5. 初学K3Cloud开发

    1.BOS中在新建的空白对象中添加一个下推按钮 1.点击“菜单集合”属性 2.在打开的窗体中,点中“工具条”,新增一个按钮 3.将新增的按钮标题改为“下推”,并配置点击事件 列表菜单增加“下推”类似, ...

  6. windows登录密码忘记了怎么办?

    利用PE工具进行进行修改密码或者重置系统密码,正对于服务器也同样试用 目前U启动制作效果还不错,黑鲨一键装机,以及老毛桃我觉得还是算了,U深度也不错 经过这么久,小编也把该测试的测试了,,小编比较懒, ...

  7. 【计算机网络】-网络层-Internet的网络层

    [计算机网络]-网络层-Internet的网络层 Internet是一组相互连接的网络或者自治系统的集合 Internet 1.存在几个主要骨干网络,骨干网络是由高带宽的线路和快速路由器构成 2.这些 ...

  8. OpenVZ平台 Google BBR加速

    前言  一直以来用的都是搬瓦工的VPS,不得不说比国内那些大厂的性价比高得不知道哪里去了.  当做梯子来用的话搬瓦工年付19.9美元的方案就够用了,网上还有一些官方优惠码(折扣6%: BWH1ZBPV ...

  9. 命名规范 camel case, pascal case, hyphen

    2019-11-08  refer : https://ux.stackexchange.com/questions/43174/update-vs-modify-vs-change-create-v ...

  10. 【原创】大叔问题定位分享(33)oozie提交任务报错ArithmeticException: / by zero

    oozie提交workflow后执行task报错: 2019-07-04 17:19:00,559 ERROR [RMCommunicator Allocator] org.apache.hadoop ...