问题
I would like to know if Boost Ptree allows parsing .xml files without having the attribute tags known. I have an XML file that will regularly be updated with new tags, new data, and even remove certain tags. These regular changes prompted me to look into parsing the file without hardcoding the attribute name. Is there any way I can read all the data in the XML file without stating the tag name? Any help is appreciated!
回答1:
Ptrees aren't ideal for parsing XML but Boost is so integrated into the rest of my project that it would be incredibly taxing to use libxml2 or another parser
Sorry, that makes no sense.
There are header-only options, and a good number of very light-weight options. See What XML parser should I use in C++?
Since Boost Property Tree is simply and clearly not the library you need the perceived cost of adding a library will be far outweighed by the cost of battling the broken abstractions and impossibilities that you will encounter.
And it seems like I do have to state the tag name when I use read_xml. There doesn't seem to be a way that I can read all the child nodes underneath a parent node and store them.
Here you go:
#include <boost/property_tree/xml_parser.hpp>
int main() {
boost::property_tree::ptree pt;
std::ifstream ifs("input.xml");
read_xml(ifs, pt);
write_xml(std::cout, pt);
}
I've also read through the boost documentation but couldn't seem to find anything.
I'd start here: https://www.boost.org/doc/libs/1_73_0/doc/html/property_tree/parsers.html#property_tree.parsers.xml_parser. Focus on the limitations. Not all limitations are explicit there (see documentation for RadidXML).
Do you know how boost get child nodes or other c++ parsers I could look at?
See first link above :)
See also:
- Search my answers for "not an xml library"
- Ditto for any Property Tree answers using XML - note that we never specify what node to read - because that's simply not possible. Many answers detail how to find nodes by some dynamic path, though, if that's what you're really looking for
- Some of my answers that use PugiXML - the majority of which are in response to questions that couldn't be handled with Boost Property Tree
- Some other libraries TinyXML, RapidXML, just to get a taste of when they are suitable/nice or not
来源:https://stackoverflow.com/questions/63136333/parsing-xmls-with-boost-ptree-w-o-tags