Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you specifically mentioned lightweight, and I haven't seen it mentioned, I'll add <a href="http://www.boost.org/doc/libs/1_43_0/doc/html/property_tree.html" rel="nofollow noreferrer">Boost.PropertyTree</a>. It uses RapidXML under the hood.</p> <p>It doesn't do anything but parse elements, but sometimes that is enough. And honestly, XML schema verification, XPath, and co. deserve to be split into separate libraries. It probably wouldn't be hard to tie in the existing C++ amalgamations that provide this extra coverage.</p> <p>Example XML file/string:</p> <pre><code>std::string xml_providers = "&lt;question&gt;" " &lt;title&gt;What’s a good way to write XML in C++?&lt;/title&gt;" " &lt;date&gt;2010/6/6&lt;/date&gt;" "&lt;/question&gt;" "&lt;providers&gt;" " &lt;provider speed=\"1\"&gt;Boost.PropertyTree&lt;/provider&gt;" " &lt;provider speed=\"6\"&gt;TinyXML++&lt;/provider&gt;" " &lt;provider speed=\"3\"&gt;PugiXML&lt;/provider&gt;" " &lt;provider speed=\"9001\"&gt;Xerxes&lt;/provider&gt;" " &lt;provider speed=\"1\"&gt;RapidXML&lt;/provider&gt;" "&lt;/providers&gt;"; </code></pre> <p><code>boost::property_tree</code> would be used like so:</p> <pre><code>using boost::property_tree::xml_parser::read_xml; using boost::property_tree::ptree; using boost::gregorian::from_string; using boost::gregorian::date; ptree doc; std::istringstream iss(xml_providers); xml_parser::read_xml(iss, doc); std::string title = doc.get&lt;std::string&gt;("question.title"); date when = from_string(doc.get&lt;std::string&gt;("question.date")); ptree providers = doc.get_child("providers"); std::size_t size = std::distance(providers.begin(), providers.end()); std::cout &lt;&lt; "Question: " &lt;&lt; title &lt;&lt; std::endl; std::cout &lt;&lt; "Asked on: " &lt;&lt; when &lt;&lt; std::endl; std::cout &lt;&lt; size &lt;&lt; " suggested XML providers" &lt;&lt; std::endl &lt;&lt; std::endl; std::cout &lt;&lt; std::left &lt;&lt; std::setw(25) &lt;&lt; "Provider" &lt;&lt; "Speed" &lt;&lt; std::endl; providers.sort(sort_by_speed); std::for_each(providers.begin(), providers.end(), send_to_cout); </code></pre> <p>And the two helper functions:</p> <pre><code>auto sort_by_speed = [](ptree::value_type&amp; a, ptree::value_type&amp; b) -&gt; bool { int a_speed = a.second.get&lt;int&gt;("&lt;xmlattr&gt;.speed"); int b_speed = b.second.get&lt;int&gt;("&lt;xmlattr&gt;.speed"); return a_speed &lt; b_speed; }; auto send_to_cout = [](ptree::value_type&amp; provider) { std::cout &lt;&lt; std::left &lt;&lt; std::setw(25) &lt;&lt; provider.second.get_value&lt;std::string&gt;() &lt;&lt; provider.second.get&lt;int&gt;("&lt;xmlattr&gt;.speed") &lt;&lt; std::endl; }; </code></pre> <p>Output:</p> <pre> Question: WhatÆs a good way to write XML in C++? Asked on: 2010-Jun-06 5 suggested XML providers Provider Speed Boost.PropertyTree 1 RapidXML 1 PugiXML 3 TinyXML++ 6 Xerxes 9001 </pre> <p>It's so easy that you don't even need to know XML!</p> <p>(Ignore the speed ratings I attributed the libraries. I made them up.)</p> <p>edit: property_tree supports exporting XML from a ptree object, of course. I wrote an example for reading XML because (1) it's the only usage I am familiar with, and (2) there are very few examples online.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload