解析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>(' ', 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>(' ', 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>(' ', 1);
//把property_tree 转为XML文件
write_xml(m_iworldDir.string(), pt, std::locale(), settings);
}