-1

I am using boost for the first time within an old code base that we have

iptree children = pt.get_child(fieldName);
for (const auto& kv : children) { 
    boost::property_tree::iptree subtree = (boost::property_tree::iptree) kv.second ;
//Recursive call
}

My problem is sometimes the fieldName doesn`t exist in the XML file and I have an exception

I tried :

boost::property_tree::iptree::assoc_iterator it = pt.find(fieldName);

but I dont know how to use the it I can`t use: if (it != null)

Any help please will be appreciated

I am using VS 2012

If it`s very complicated is there any other way to read a XML with nested nodes? I am working on that since 3 days

This is an Example of the XML

<?xml version="1.0" encoding="utf-8"?>
<nodeA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <nodeA.1>This is the Adresse</nodeA.1>
    <nodeA.2>
    <node1>
      <node1.1>
        <node1.1.1>Female</node1.1.1>
        <node1.1.2>23</node1.1.2>
        <node1.1.3>Engineer</node1.1.3>
      </node1.1>
      <node1.2>
        <node1.2.1>Female</node1.2.1>
        <node1.2.2>35</node1.2.2>
        <node1.2.3>Doctors</node1.2.3>
      </node1.2>
    </node1>
</nodeA.2>
<nodeA.3>Car 1</nodeA.3>
</nodeA>
melom
  • 107
  • 1
  • 13
  • _"is there any other way to read a XML"_ - lots. Tell me what you want to achieve (ultimately, not some - sightly relevant - step in your current solutuon) and I'll be glad to give you my preferred methods – sehe Nov 06 '14 at 08:54
  • @sehe I need to read a XML passing in my function, it`s not always the same XML their some nodes that can exist but on another XML file they may not. So I have an architecture of fields and I`m verifying if these fields exist in the XML or not if they exist I`ll copy their values, if not I`ll skip it. – melom Nov 06 '14 at 13:22
  • Sounds like a job for XSLT or maybe Xpath in your code. I think you should ask the real question here, so you we can show you how one could achieve that goal (just give sample input/output and what you've tried, which is already partly in this question). – sehe Nov 06 '14 at 13:29
  • For reference: http://xyproblem.info/ – sehe Nov 06 '14 at 13:30
  • @sehe I added an example I need to copy these values to an MFC application in a Form – melom Nov 06 '14 at 14:16
  • WAT. If it's MFC, can't just use MSXML or .NET Xml parsers using c++-cli? – sehe Nov 06 '14 at 14:17
  • Also, I asked you to show what you wanted to achieve. You've shown us just an XML document. So, you've already achieved that. What's next? – sehe Nov 06 '14 at 14:19
  • @sehe this is a code heritage and it`s first time I work on it, since it was already there they estimated to 4 days works and only 1 day to adapt the xml reading file, I never use MSXML, I just verify and the project is : No Common Language Runtime Support and for MFC I have Use Standard Windows Libraries – melom Nov 06 '14 at 14:22
  • The goal of this is to read the XML and send the values to the classes in the project and then show it in the applications, we may receive different file format from different clients – melom Nov 06 '14 at 14:25
  • XSLT. And since you refuse to say what the logic for the required transformation is, I'll simply let this question go. – sehe Nov 06 '14 at 14:34
  • @sehe my language is not English so I`m not understanding well, I have an XML file and a defined structure with google protocol buffer message and I need to parse the XML file with boost recursively and create the google protocol buffer structure set of messages, I don`t know if it`s correct what I explain – melom Nov 06 '14 at 15:01

1 Answers1

0

Use pt.get_child_optional(...) to prevent an exception. pt.find(...) returns an iterator which compares true to pt.not_found() on failure.

EDIT: How to use boost::optional<--->

boost::optional< iptree & > chl =  pt.get_child_optional(fieldname); 

    if(chl) { 
      for( auto a : *chl ) 
        std::cerr << ":" << a.first << ":" << std::endl; 
    } 
Oncaphillis
  • 1,888
  • 13
  • 15
  • I tried pt.get_child_optional but I had an error on the for (const auto& kv : children) and even if there is child if I did the if (child) it return false – melom Nov 05 '14 at 20:14
  • @melom Of yourse you have to check your boost::optional for validity befor iterating. I've expanded the example. – Oncaphillis Nov 05 '14 at 22:05
  • thank you I don`t have the error, but if you see my updated example the node1 is not recognize as having a child !! even if I have 2 nested child and sometimes I have but for my initial error your code works fine – melom Nov 06 '14 at 14:18
  • @melom I would make a new request with running sample code out of it. – Oncaphillis Nov 06 '14 at 16:23