Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:22:23+00:00 2026-06-14T07:22:23+00:00

I’m attempting to parse the following SVG (XML) markup with Boost C++’s ptree… The

  • 0

I’m attempting to parse the following SVG (XML) markup with Boost C++’s ptree…

The SVG (XML)

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg height="395.275590551181" version="1.1"
     viewBox="0 0 757.48031496063 395.275590551181" xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink" style="background-color: #ffffff;">
<desc>Some Description</desc>
<g visibility="hidden" pointer-events="all">
    <rect x="0" y="0" width="100%" height="100%" fill="none" />
</g>
<g>
    <path d="M 5 56 L 5 14 L 8 11 L 34 11 Z" />
    <path d="M 10 100 L 10 140 L 89 131 L 324 2 Z" />
</g>
<g>
    <path d="M 20 130 L 1 1 L 89 130 L 34 2 Z" />
</g>
</svg>

The C++ Code:

    typedef boost::property_tree::ptree::value_type vt;
    boost::property_tree::ptree root;
    read_xml("My_SVG.svg", root);
    BOOST_FOREACH (vt const &nodes, root.get_child("svg")) {
        //only action on g's and not desc, comments, etc.
        if(nodes.first=="g"){
            boost::property_tree::ptree g = nodes.second;
            //only action on g's that contain paths, not g->rect, for example.
            if(g.count("path") != 0){
                BOOST_FOREACH (vt const &p, g.get_child("path")) {
                    std::cout << p.second.get("d", "false") << std::endl;
                }
            }
        }
    }

The Output:

M 5 56 L 5 14 L 8 11 L 34 11 Z
M 20 130 L 1 1 L 89 130 L 34 2 Z

The Problem:

It compiles fine but it’s not picking up the M 10 100 L 10 140 L 89 131 L 324 2 Z entry. Why doesn’t the second BOOST_FOREACH step through each <path> and cout it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:22:25+00:00Added an answer on June 14, 2026 at 7:22 am

    The second BOOST_FOREACH is calling g.get_child("path") which returns the first child of g called path. You need to iterate over all of the children with the key that you are looking for.

    This can be achieved using:

    /** Find the range of children that have the given key. */
    std::pair<assoc_iterator, assoc_iterator> 
       ptree::equal_range(const key_type &key);
    
    /** Count the number of direct children with the given key. */
    size_type count(const key_type &key) const;
    

    e.g.

      typedef boost::property_tree::ptree::value_type vt;
    BOOST_FOREACH (vt const &nodes, pt.get_child("svg")) 
    {
        //only action on g's and not desc, comments, etc.
        if(nodes.first=="g")
        {
            const boost::property_tree::ptree& g = nodes.second;
            //only action on g's that contain paths, not g->rect, for example.
            if(g.count("path") != 0)
            {
                std::pair< ptree::const_assoc_iterator,
                           ptree::const_assoc_iterator > bounds = g.equal_range( "path" );
    
                for( ptree::const_assoc_iterator it = bounds.first; it != bounds.second; ++it )
                {
                    std::cout  << it->first << " : ";
    
                    const ptree& d = it->second.get_child( "<xmlattr>.d" );
                    std::cout << d.get_value<std::string>() << "\n";
                }
            }
        }
    }
    

    I find the following code useful when working with ptree, it will display your entire parsed ptree:

    void display_ptree(ptree const& pt)
    {
        BOOST_FOREACH( const ptree::value_type& v, pt )
        {e 
            std::cout << v.first << ": " << v.second.get_value<std::string>() << "\n";
            display_ptree( v.second );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i got an object with contents of html markup in it, for example: string
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.