Boost XML Datei auswerten
-
Hallo zusammen,
ich muss eine XML Datei auswerten und komme hierbei nicht weiter.
<ReadValues> <Group name="General Auto"> <Result name="General.KFZ" value="VWC" state="Good"/> <Result name="General.Modell" value="Golf 5" state="Good"/> <Result name="General.Baujahr" value="2001" state="Good"/> <Result name="General.Kilometer" value="160000" state="Good"/> <Result name="General.Halter.1" value="Franz" state="Good"/> <Result name="General.Halter.2" value="Mueller" state="Good"/> </Group> <Group name="Device Zustand"> <Result name="Status.Zustand1" value="geplegt"/> <Result name="Status.Farbe" value="Grau Metalic" state="Good"/> </Group> </ReadValues>
Wie komme ich an die value ran?
Bisher habe ich sowas probiert
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <boost/foreach.hpp> #include <iostream>
boost::property_tree::ptree tree; boost::property_tree::read_xml("C:/xml/auto.xml", tree); boost::property_tree::ptree formats = tree.get_child("ReadValues.Group"); BOOST_FOREACH(boost::property_tree::ptree::value_type const& f, formats) { if (f.first == "Result") { Results s; s.roomName = f.second.data(); std::cout << s.roomName << std::endl; std::cin.get(); etc.
Am liebsten möchte euch alle values untereinander ausgegeben bekommen.
C++ VisualStudio 2017
-
Hallo,
eventuell hilft dir ja https://stackoverflow.com/questions/14010473/parsing-xml-attributes-with-boost weiter.
-
Nimm doch einfach pugixml und mach es nicht mit boost kompliziert.
-
Danke inflames!
Bin jetzt weiter gekommen.
Nur in die 2. Group komme ich nicht rein "Device Zustand".Wenn ich die Groups in der Quelldatei umbenne funktioniert es.
<Group1 name="General Auto">
<Group2 name="Device Zustand">const ptree & formats = tree.get_child("ReadValues.Group1", empty_ptree()); etc. const ptree & formats = tree.get_child("ReadValues.Group2", empty_ptree()); etc.
Die XML Datei ist leider sehr verschachtelt....
-
so gehts bei mir
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> struct Result { std::string name; std::string value; std::string state; Result( std::string n, std::string v, std::string s = "" ): name( n ), value( v ), state( s ) { } }; void LoadResult( const boost::property_tree::ptree& tree, std::vector<Result>& vec ) { for ( const auto& result : tree ) { if ( result.first == "Result" ) { Result res( result.second.get<std::string>( "<xmlattr>.name" ), result.second.get<std::string>( "<xmlattr>.value" ) ); auto state = result.second.get_optional<std::string>( "<xmlattr>.state" ); if ( state.is_initialized( ) ) res.state = state.get( ); vec.push_back( res ); } } } int main( int, char*[] ) { boost::property_tree::ptree tree; boost::property_tree::read_xml( "test.xml", tree ); std::vector<Result> general; std::vector<Result> device; for ( const auto& groups : tree.get_child( "ReadValues" ) ) { if ( groups.second.get<std::string>( "<xmlattr>.name" ) == "General Auto" ) { LoadResult( groups.second, general ); } if ( groups.second.get<std::string>( "<xmlattr>.name" ) == "Device Zustand" ) { LoadResult( groups.second, device ); } } return 1; }