63244

c++ simple parse with boost xml with property tree

I have this question about the boost xml parsing:

here is a piece of my Xml:

<Clients> <Client name="Alfred" /> <Client name="Thomas" /> <Client name="Mark" /> </Clients>

and I read the name with this code:

std::string name = pt.get<std::string>("Clients.Client.<xmlattr>.name, "No name");

and works fine, but retrieve always the first node..

Is there a way to get the second, third node without looping?

thanks

Answer1:

There's no facility to query multi-valued keys in Property Tree. (Partly because most of the supported backend formats do not officially support duplicate keys).

However, you can iterate through child elements, so you can implement your own query, like so:

for (auto& child : pt.get_child("Clients")) if (child.first == "Client") std::cout << child.second.get<std::string>("<xmlattr>.name", "No name") << "\n";

See fully working sample <strong>Live On Coliru</strong></kbd>:

#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <sstream> #include <iostream> using boost::property_tree::ptree; int main() { std::stringstream ss("<Clients>\n" " <Client name=\"Alfred\" />\n" " <Client name=\"Thomas\" />\n" " <Client name=\"Mark\" />\n" "</Clients>"); ptree pt; boost::property_tree::read_xml(ss, pt); for (auto& child : pt.get_child("Clients")) { if (child.first == "Client") std::cout << child.second.get<std::string>("<xmlattr>.name", "No name") << "\n"; } };

Recommend

  • ColdFusion - DateTime Format with GMT offset
  • jquery .height() and .width() on span tag gets inconsistent results
  • Find all paths in a maze using DFS [duplicate]
  • Web browser lock-down: How to?
  • Monotouch crashes with NullReferenceException on non nullable object
  • using html data-attributes as css-variable (i.e. text-shadow)
  • How to retrieve information from antrun back to maven?
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Simple linked list-C
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • Hide HTML elements without javascript, only CSS
  • bad substitution shell- trying to use variable as name of array
  • CakePHP ACL tutorial initDB function warnings
  • Not able to aggregate on nested fields in elasticsearch
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Unable to get column index with table.getColumn method using custom table Model
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Database structure design with variable amounts of fields
  • Meteor helpers not available in Angular template
  • Can I display google adwords (AdView) in javafx on android
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • How do I fake an specific browser client when using Java's Net library?
  • Sony Xperia Z Tablet not found by adb
  • How to get a value (ex: baseURL) in every Karate feature?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Can I make an Android app that runs a web view in Chrome 39?
  • Javascript + PHP Encryption with pidCrypt
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • AT Commands to Send SMS not working in Windows 8.1
  • How do I rollback to a specific git commit
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • apache spark aggregate function using min value
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?