如何使用 boost lib 删除最后一个 xml 节点及其子节点?
How to delete the last xml node and its child with boost lib?
我想从 XML 文件中删除一个节点。
XML 字符串看起来像:
<BookData>
<Book><Author>A1<Author><Name>B1</Name><Price>C1</Price></Book>
<Book><Author>A2<Author><Name>B2</Name><Price>C2</Price></Book>
...
<Book><Author>A(n-1)<Author><Name>B(n-1)</Name><Price>C(n-1)</Price></Book>
<Book><Author>A(n)<Author><Name>B(n)</Name><Price>C(n)</Price></Book>
</BookData>
我希望它能这样结束。
<BookData>
<Book><Author>A1<Author><Name>B1</Name><Price>C1</Price></Book>
<Book><Author>A2<Author><Name>B2</Name><Price>C2</Price></Book>
...
<Book><Author>A(n-1)<Author><Name>B(n-1)</Name><Price>C(n-1)</Price></Book>
</BookData>
我如何使用 boost 库来做到这一点?
您可以使用反向迭代器 rbegin()
,但转换为其基本迭代器总是有点棘手:
auto& root = pt.get_child("BookData");
root.erase(std::next(root.rbegin()).base());
cppreference has a neat illustration:
#include <iostream>
#include <boost/property_tree/xml_parser.hpp>
int main() {
std::istringstream iss(R"(
<BookData>
<Book><Author>A1</Author><Name>B1</Name><Price>C1</Price></Book>
<Book><Author>A2</Author><Name>B2</Name><Price>C2</Price></Book>
<Book><Author>A(n-1)</Author><Name>B(n-1)</Name><Price>C(n-1)</Price></Book>
<Book><Author>A(n)</Author><Name>B(n)</Name><Price>C(n)</Price></Book>
</BookData>
)");
boost::property_tree::ptree pt;
read_xml(iss, pt);
auto& root = pt.get_child("BookData");
root.erase(std::next(root.rbegin()).base());
write_xml(std::cout, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4));
}
版画
<?xml version="1.0" encoding="utf-8"?>
<BookData>
<Book>
<Author>A1</Author>
<Name>B1</Name>
<Price>C1</Price>
</Book>
<Book>
<Author>A2</Author>
<Name>B2</Name>
<Price>C2</Price>
</Book>
<Book>
<Author>A(n-1)</Author>
<Name>B(n-1)</Name>
<Price>C(n-1)</Price>
</Book>
</BookData>
我想从 XML 文件中删除一个节点。
XML 字符串看起来像:
<BookData>
<Book><Author>A1<Author><Name>B1</Name><Price>C1</Price></Book>
<Book><Author>A2<Author><Name>B2</Name><Price>C2</Price></Book>
...
<Book><Author>A(n-1)<Author><Name>B(n-1)</Name><Price>C(n-1)</Price></Book>
<Book><Author>A(n)<Author><Name>B(n)</Name><Price>C(n)</Price></Book>
</BookData>
我希望它能这样结束。
<BookData>
<Book><Author>A1<Author><Name>B1</Name><Price>C1</Price></Book>
<Book><Author>A2<Author><Name>B2</Name><Price>C2</Price></Book>
...
<Book><Author>A(n-1)<Author><Name>B(n-1)</Name><Price>C(n-1)</Price></Book>
</BookData>
我如何使用 boost 库来做到这一点?
您可以使用反向迭代器 rbegin()
,但转换为其基本迭代器总是有点棘手:
auto& root = pt.get_child("BookData");
root.erase(std::next(root.rbegin()).base());
cppreference has a neat illustration:
#include <iostream>
#include <boost/property_tree/xml_parser.hpp>
int main() {
std::istringstream iss(R"(
<BookData>
<Book><Author>A1</Author><Name>B1</Name><Price>C1</Price></Book>
<Book><Author>A2</Author><Name>B2</Name><Price>C2</Price></Book>
<Book><Author>A(n-1)</Author><Name>B(n-1)</Name><Price>C(n-1)</Price></Book>
<Book><Author>A(n)</Author><Name>B(n)</Name><Price>C(n)</Price></Book>
</BookData>
)");
boost::property_tree::ptree pt;
read_xml(iss, pt);
auto& root = pt.get_child("BookData");
root.erase(std::next(root.rbegin()).base());
write_xml(std::cout, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4));
}
版画
<?xml version="1.0" encoding="utf-8"?>
<BookData>
<Book>
<Author>A1</Author>
<Name>B1</Name>
<Price>C1</Price>
</Book>
<Book>
<Author>A2</Author>
<Name>B2</Name>
<Price>C2</Price>
</Book>
<Book>
<Author>A(n-1)</Author>
<Name>B(n-1)</Name>
<Price>C(n-1)</Price>
</Book>
</BookData>