2

Trying to get the content from the xml file using boost xml parser using c++..

opencv.xml

<opencv_storage>
     <labels type_id="opencv-matrix">
          <data>0 0 0 0 1 1 0 0</data>
     </labels>
</opencv_storage>

C++ Code Snippet

using boost::property_tree::ptree;
    ptree pt;
    boost::property_tree::read_xml("opencv.xml", pt);

    std::string m_file = pt.get<std::string>("opencv_storage.labels type_id=\"opencv-matrix\".data");

    std::cout<<"m_file "<<m_file<<std::endl;

While executing, the program throws an exception :

No such node (opencv_storage.labels type_id="opencv-matrix".data)

I doubt, a white space prevails between labels and type_id

Thanks in advance, any help would be appreciated, since I am trying to get used to boost.

2
  • The name of the node is labels, the type_id field is a separate attribute for the node. Just like in HTML where nodes can have attributes. Try just "opencv_storage.labels.data". Commented Sep 21, 2015 at 8:06
  • @Joachim It works .. Very simple.. Commented Sep 21, 2015 at 8:15

1 Answer 1

1

Of course it doesn't. Whitespace in element names is illegal in XML.

What you actually want is attributes: Parsing XML Attributes with Boost

Or, better yet, you want to use an XML parser, here: What XML parser should I use in C++?


If somehow you want to use poperty tree (are you sure?) look here:

The enumerate-path function can - obviously - be used for XML too, since it takes a ptree

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.