Abhisheyk Deb
Abhisheyk Deb

Reputation: 35

Boost XML Parser for c++

I an new to using boost c++ libraries, wanted to use it to parse a xml document, but I am having trouble figuring out the logic.

<?xml version="1.0"?>
<GCC_XML version="0.9.0" cvs_revision="1.140">
  <Function id="_6" name="A" returns="_12" context="_1" location="f1:1" file="f1" line="1" mangled="_Z1Aif">
    <Argument name="a" type="_12" location="f1:1" file="f1" line="1"/>
    <Argument name="c" type="_13" location="f1:1" file="f1" line="1"/>
  </Function>
  <Function id="_7" name="B" returns="_14" context="_1" location="f1:7" file="f1" line="7" mangled="_Z1Bf">
    <Argument name="d" type="_13" location="f1:7" file="f1" line="7"/>
  </Function>
</GCC_XML>

If I want to access the Argument tags for each function tag, how should I go about it? I was able to access the function tags as follows.

    BOOST_FOREACH( ptree::value_type const& v, pt.get_child("GCC_XML") ) {
        if(v.first == "Function") {
          cout << "Function name : " << v.second.get_child("<xmlattr>.name").data() << endl;
          cout << "Function return type : " << v.second.get_child("<xmlattr>.returns").data() << endl;
}

Upvotes: 1

Views: 391

Answers (1)

sehe
sehe

Reputation: 392931

Boost has no XML library. It has a property tree library.

That out of the way, just do exactly the same:

BOOST_FOREACH( ptree::value_type const& a, v ) {
    if(a.first == "Argument") {
      cout << "Argument name : " << a.second.get_child("<xmlattr>.name").data() << endl;
      cout << "Argument type : " << a.second.get_child("<xmlattr>.type").data() << endl;
    }
}

Upvotes: 1

Related Questions