解析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. 基于freescale i.Mx6(ARM)的阿里云oss调试记录

    交叉编译阿里OSS调试记录 1.1 开通oss服务 具体参考以下链接: https://help.aliyun.com/document_detail/31884.html?spm=a2c4g.111 ...

  2. Capacity To Ship Packages Within D Days

    A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...

  3. Spring实战(十二) Spring中注入AspectJ切面

    1.Spring AOP与AspectJ Spring AOP与AspectJ相比,是一个功能比较弱的AOP解决方案. AspectJ提供了许多它不能支持的类型切点,如在创建对象时应用通知,构造器切点 ...

  4. 关闭钩子(shutdown hook)的作用以及在Tomcat中的使用

    在很多实际应用环境中,当用户关了应用程序时,需要做一些善后清理工作,但问题是,用户有时并不会按照推荐的方法关闭应用程序,很有可能不做清理工作,例如在Tomcat的部署应用中,通过实例化一个Server ...

  5. MVC中的Action过滤器

    Action过滤器可以用在调用动作方法之前或之后,执行一些特殊的逻辑,比如用登录验证: Action过滤器实现IActionFilter接口,该接口有两个方法: public interface IA ...

  6. 题解 P2280 【[HNOI2003]激光炸弹】

    题目链接: https://www.luogu.org/problemnew/show/P2280 思路: 简单的二维前缀和,最后扫描一遍求 max(ans,f[i][j]+f[i-r][j-r]-f ...

  7. java后台读取配置文件

    前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家: 先附上代码吧: package com.shafei.util; import java.io. ...

  8. 高德地图API-设置考勤范围

    <template> <div class="page-setting-setgps"> <!--head--> <div class=& ...

  9. Jupyter Notebook不能自动打开浏览器

    安装了 Winpython,运行Jupyter Notebook.exe或Jupyter lab.exe,总是不能自动打开浏览器,提示"no web browser found" ...

  10. python之字典一

    字典的定义: 前面我们说过列表,它适合于将值组织到一个结构中并且通过编号对其进行引用.字典则是通过名字来引用值的数据结构,并且把这种数据结构称为映射,字典中的值没有特殊的顺序,都存储在一个特定的键(k ...