Boost 解析xml 插入Item

2021-08-21 07:38:01 字數 4485 閱讀 8229

xml格式為

<?

xml version="1.0" encoding="utf-8"

?>

<

config

>

<

item

name

="a"

desc

="">

<

childitem

name

="name"

desc

=""datatype

="string"

>11111

childitem

>

<

childitem

name

="subject"

desc

=""datatype

="string"

>22222

childitem

>

<

childitem

name

="desc"

desc

=""datatype

="string"

>33333

childitem

>

<

childitem

name

="state"

desc

=""datatype

="int"

>444444

childitem

>

<

childitem

name

="id"

desc

=""datatype

="int"

>55555

childitem

>

item

>

config

>

需求:將另一段同樣格式xml的item插入到現在這個item下面

<?

xml version="1.0" encoding="utf-8"

?>

<

config

>

<

item

name

="a"

desc

="">

<

childitem

name

="name"

desc

=""datatype

="string"

>11111

childitem

>

<

childitem

name

="subject"

desc

=""datatype

="string"

>22222

childitem

>

<

childitem

name

="desc"

desc

=""datatype

="string"

>33333

childitem

>

<

childitem

name

="state"

desc

=""datatype

="int"

>444444

childitem

>

<

childitem

name

="id"

desc

=""datatype

="int"

>55555

childitem

>

item

>

<

item

name

="b"

desc

="">

<

childitem

name

="name"

desc

=""datatype

="string"

>11111

childitem

>

<

childitem

name

="subject"

desc

=""datatype

="string"

>22222

childitem

>

<

childitem

name

="desc"

desc

=""datatype

="string"

>33333

childitem

>

<

childitem

name

="state"

desc

=""datatype

="int"

>444444

childitem

>

<

childitem

name

="id"

desc

=""datatype

="int"

>55555

childitem

>

item

>

config

>

這樣**:

wstring filepath = m_resourcefilepath.m_trainingresourcepath + pstrid +m_resourcefilepath.m_trainingxmlfilepath;

cplsimxmlconfigurationfile m_manageconfig;

//m_manageconfig.m_filename;

m_manageconfig.initconfigfile(plsimlocale::wstringtostring(filepath));

wptree read_pt;

//原來的xml直接複製過去,因為是第乙個迴圈所以只會複製第乙個xml

wptree copy_pt;

copy_pt =m_manageconfig.m_pt;

try std::locale utf8locale(std::locale(),

new std::codecvt_utf8);

auto settings = boost::property_tree::xml_writer_make_settings(l'

\t', 1

); write_xml(plsimlocale::wstringtostring(m_resourcefilepath.m_resourcestorexmlpath), m_manageconfig.m_pt, utf8locale, settings);

}else

//去掉從庫檔案裡讀的頭

read_pt = read_pt.get_child(l"

config");

//去掉從配置檔案中讀的頭

copy_pt = copy_pt.get_child(l"

config.item");

//合併xml

wptree array_pt;

array_pt.add_child(l

"item

", read_pt);

array_pt = array_pt.get_child(l"

item");

auto& b = array_pt.add_child(l"

item

", copy_pt);

b.put(l

".name

", strconfigattrname);

b.put(l

".desc

", l""

); wptree all_pt;

all_pt.put_child(l

"config

", array_pt);

std::locale utf8locale(std::locale(),

new std::codecvt_utf8);

auto settings = boost::property_tree::xml_writer_make_settings(l'

\t', 1

); write_xml(plsimlocale::wstringtostring(m_resourcefilepath.m_resourcestorexmlpath), all_pt, utf8locale, settings);}}

}catch (boost::property_tree::ptree_bad_path&e)

catch (boost::property_tree::ptree_bad_data&e)

BOOST 解析,修改,生成xml樣例

解析iworld xml,拿到entity和visiblevolume的資料 int parseiworlds readxml const bpath dir catch const std exception e ptree ptchild pt.get child world.levels.le...

使用Boost讀取xml

boost中提供了對配置檔案讀取的支援,它就是 property tree。basic ptree 是property tree的核心基礎。其介面像std list。可以執行很多基本的元素操作,比如使用begin end 等。此外還加入了操作屬性樹的get get child get value d...

使用boost讀取XML檔案

boost中提供了對配置檔案讀取的支援,它就是 property tree。basic ptree 是property tree的核心基礎。其介面像std list。可以執行很多基本的元素操作,比如使用begin end 等。此外還加入了操作屬性樹的get get child get value d...