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. ...
随机推荐
- Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- *【Python】【demo实验26】【练习实例】【递归方法的使用】
原题: 利用递归方法求5! 原题给出的解答: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 利用递归方法求5! def fa ...
- better-scroll踩坑合集
better-scroll踩坑合集:https://www.jianshu.com/p/6338a8033281
- 并不对劲的复健训练-bzoj5339:loj2578:p4593:[TJOI2018]教科书般的亵渎
题目大意 题目链接 题解 先将\(a\)排序. \(k\)看上去等于怪的血量连续段的个数,但是要注意当存在\(a_i+1=a_{i+1}\)时,虽然它们之间的连续段为空,但是还要算上:而当\(a_m= ...
- shiro过滤器机制
shiro内置过滤器介绍 https://blog.csdn.net/qq_35608780/article/details/71703197 Shiro的Filter机制详解---源码分析 http ...
- 管家婆crm9.2 sp2升级问题求助及解决方案
升级过程中发生如下问题: 弹出对话框1:升级完成,但是有错误产生. 弹出对话框2:升级数据库发生错误:An attempt was made to load an assembly from a ne ...
- linq to sql之Distinct
Distict用来排除相同序列中元素的,对于基础类型,可以直接使用Distinct,如:int[] a = {1, 2, 2, 3, 3, 3, 4};var reslut = a.Distinct( ...
- 适配方案(四)适配的基础知识之单位、分辨率、viewport
适配的基础知识 一.理解单位 px.pt.pc.sp.em.rem.dpr.dp.dip.ppi.dpi.ldpi.mdpi.hdpi.xhdpi.xxhdpi 如果你是ios开发,你需要了解的单位: ...
- spring ioc aop 理解
OC,依赖倒置的意思,所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果 ...
- Java学习笔记【五、字符串】
String类 11种构造,不一一列举 常用方法 s.length() 返回字符串长度 s1.contact(s2) 连接s1.s2 String.format("aaa %f bbb %d ...