您可以使用 Boost Filesystem 支持的 Boost Iostreamsfile_descriptor_sink
设备wpath
:
#include <boost/property_tree/xml_parser.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
namespace pt = boost::property_tree;
namespace io = boost::iostreams;
namespace fs = boost::filesystem;
int main()
{
fs::wpath const fname = L"test.xml";
io::file_descriptor_source fs(fname);
io::stream<io::file_descriptor_source> fsstream(fs);
pt::ptree xml;
pt::read_xml(fsstream, xml);
for (auto const& node : xml.get_child("root"))
std::cout << node.first << ": " << node.second.get_value<std::string>() << "\n";
}
在 Coliru上查看它使用输入文件的地方:
<root>
<child nodetype="element" with="attributes">monkey show</child>
<child nodetype="element">monkey do</child>
</root>
并打印:
child: monkey show
child: monkey do