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);
    
}

转载于:https://www.cnblogs.com/SeekHit/p/7202051.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改一个 XML 文件中的某一项,可以使用 Boost XML 提供的 API 来实现: 1. 首先,需要读取 XML 文件,并将其解析XML 对象,可以使用 `boost::property_tree::xml_parser::read_xml` 函数来实现。 ```c++ boost::property_tree::ptree pt; boost::property_tree::xml_parser::read_xml("example.xml", pt); ``` 2. 然后,需要找到需要修改XML 节点,可以使用 `boost::property_tree::ptree::get_child` 函数获取指定节点的子节点。 ```c++ boost::property_tree::ptree& node = pt.get_child("root.node_to_modify"); ``` 3. 接下来,可以通过修改节点的值来对其进行修改。 ```c++ node.put_value("new_value"); ``` 4. 最后,需要将修改后的 XML 对象重新写入文件,可以使用 `boost::property_tree::xml_parser::write_xml` 函数来实现。 ```c++ boost::property_tree::xml_parser::write_xml("example.xml", pt); ``` 完整的代码如下: ```c++ #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <iostream> int main() { boost::property_tree::ptree pt; boost::property_tree::xml_parser::read_xml("example.xml", pt); boost::property_tree::ptree& node = pt.get_child("root.node_to_modify"); node.put_value("new_value"); boost::property_tree::xml_parser::write_xml("example.xml", pt); return 0; } ``` 注意,以上代码只是修改了内存中的 XML 对象,并没有直接修改文件。如果需要直接修改文件,可以在写入文件之前先将原文件备份,然后再写入修改后的 XML 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值