Reading XML attributes using Boost and FOREACH

Feb 9, 2012 at 6:17pm
So I've got code that works if I treat what I want to be attributes as elements. Here is an example of what I'm talking about.

SomeFile.xml
1
2
3
4
5
6
7
8
//...
<Node>
   <Type>XET</Type>
   <Reference>00110232</Reference>
   <Date>02-09-2012</Date>
   //..More stuff in node...
</Node>
<Node>//... Thousands of nodes ... 


What I would like to do is have the Type and Reference be an attribute for Node.

1
2
<Node Type="XET" Reference="00110232">
//... 


In my code I've got a loop like this, which works fine:
1
2
3
4
5
6
7
8
9
10
11
BOOST_FOREACH(pt_value_type &ele, pt.get_child(element::root))
{
  if(ele.first == element::node)
  {				
    std::string type = ele.second.get_child(element::name).data();
    std::string reference =  ele.second.get_child(element::reference).data();
    //... Doing other things
    parseNodeDefaults(ele, tree);
    //...
  }
}


What I really want to do is still loop but while parsing pull out or check the attributes for the node. My problem is the Boost::property_tree::ptree::value_type doesn't seem to have a mechanism for me to do this. I know how I can pull the value out of a ptree but not a ptree::value_type. Is there a way to do what I'm describing with a value_type?
Feb 17, 2012 at 9:23pm
For anyone that needs to know I figured this out (And I'm slapping myself in the face for not sooner)


A file who has attributes.. e.g.
1
2
<Node Type="XET" Reference="00110232">
//...  


Using the example above... can now get the attributes for the value_type
1
2
3
4
5
6
7
8
9
10
11
BOOST_FOREACH(pt_value_type &ele, pt.get_child(element::root))
{
  if(ele.first == element::node)
  {				
    std::string type = ele.second.get_child("<xmlattr>.type").data(); //Getting attribute instead of child element
    std::string reference =  ele.second.get_child("<xmlattr>.Reference").data(); //Getting attribute instead of child element
    //... Doing other things
    parseNodeDefaults(ele, tree);
    //...
  }
}
Topic archived. No new replies allowed.