BOOST 解析,修改,生成xml样例
解析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样例的更多相关文章
- maven中使用dom4j解析、生成XML的简易方法
此片文章主要写一些关于如何在maven工程中使用dom4j来解析或生成XML的建议方法,实际可使用的写法不仅限于如下所写的样例代码.此处进攻快速入手和提供思路使用. 首先配置pom.xml中的依赖的包 ...
- SAX解析和生成XML文档
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui031 ...
- 使用Pull解析器生成XML文件和读取xml文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- 使用Pull解析器生成XML文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- java使用xsd校验xml样例
知识点:XSD文件是指XML结构定义 ( XML Schemas Definition )文件,是DTD的替代品.可以用一个指定的XML Schema来验证某个XML文档,以检查该XML文档是否符合其 ...
- Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml) -- 样例(6)
managed-schema 样例: <?xml version="1.0" encoding="UTF-8" ?> <!-- License ...
- python 解析与生成xml
xml.etree.ElementTree模块为xml文件的提取和建立提供了简单有效的API.下文中使用ET来代表xml.etree.ElementTree模块. XML是一种内在的分层的数据形式,展 ...
- JavaWeb_使用dom4j解析、生成XML文件
dom4j 官网 xml解析DOM文档对象模型(树形结构) DOM方式解析:把xml文档加载到内存形成树形结构,可以进行增删改的操作 Learn 使用dom4j解析文件"NewFile. ...
- java 解析并生成 XML
在 java 中使用 Dom4j 解析 XML 对 XML 文件的解析,通常使用的是 Dom4j 和 jdom 作为XML解析工具. 在此只介绍下 Dom4j 对 XML 文件的解析使用方法. 1. ...
随机推荐
- 20个「MySQL」经典面试题,答对转dba 2w+「附答案」
1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 2.MySQL中myisam与innodb的区别,至少5点 (1).问5点不同: (2).innodb引擎的4大特性 (3). ...
- docker运行报错docker0: iptables: No chain/target/match by that name.
转自:https://blog.csdn.net/wohaqiyi/article/details/84450562 docker运行报错docker0: iptables: No chain/tar ...
- Selenium IDE for firefox
第一次接触selenium. 首先, selenium支持的 Firefox版本是17.0~34.x. 打开火狐历史版本:http://ftp.mozilla.org/pub/firefox/rele ...
- Web开发Flask框架学习笔记
Python 是一种跨平台的[计算机程序设计语言],是一种面向对象的动态类型语言,Python是纯粹的自由软件,源代码和解释器CPython遵循 GPL(GNU General Public Lice ...
- 项目实践 hrm项目的设计过程
人事管理系统的设计过程 一.数据库表和持久化类 1.1 进行需求分析,根据功能模块设计数据库表 1.2 设计持久化实体 面向对象分析,即根据系统需求提取出应用中的对象,将这些对象抽象成类,再抽 ...
- java——ArrayList中常见方法用法
package com.xt.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List; p ...
- iframe父子页面通信
一.同域下父子页面的通信 1.父页面调用子iframe页面 (1)通过iframe的Id获取子页面的dom,然后通过内置属性contentWindow取得子窗口的window对象,此方法兼容各个浏览器 ...
- ASP.NET_正则表达式_匹配HTML中的一行或多行
一.匹配数字串/flash/([0-9]+).htm 二.匹配不含双引号的字符串<p class=\"w490\">([^\"]+)</p> 三. ...
- webpack配置不同打包配置
生成环境与开发环境打包配置 使用package.json配置npm run开启不同的打包配置 ...webpack基本使用最后一篇博客 在上一篇博客中详细的演示了webpack开启本地服务和热更新,这 ...
- rsync 文件同步 linux
3.rsync+sersync更快更节约资源实现web数据同步4.unison+inotify实现web数据双向同步