查找XML与升压关键 [英] Find Key in XML with Boost

查看:127
本文介绍了查找XML与升压关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是旧code基地内首次提升,我们有

  iptree孩子= pt.get_child(字段名);
对于(const的汽车和放大器; KV:儿童){
    提高:: property_tree :: iptree子树=(的boost :: property_tree :: iptree)kv.second;
//递归调用
}

我的问题是有时字段名不`吨存在XML文件中,我有一个例外

我试过:

 的boost :: property_tree :: iptree :: assoc_iterator吧= pt.find(字段名);

但我不知道如何使用它,我不能老是用的:if(!它= NULL)

任何帮助,请将AP preciated

我使用VS 2012

如果它`很复杂的是还有没有其他的方式来阅读嵌套节点的XML?我的工作是,已有3天

这是XML的例子

 <?XML版本=1.0编码=UTF-8&GT?;
< nodeA上的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
  &所述; nodeA.1>这是住址&下; /nodeA.1>
    < nodeA.2>
    <&节点1 GT;
      < node1.1>
        < node1.1.1>女< /node1.1.1>
        &所述; node1.1.2> 23℃; /node1.1.2>
        < node1.1.3>&工程师LT; /node1.1.3>
      < /node1.1>
      < node1.2>
        < node1.2.1>女< /node1.2.1>
        &所述; node1.2.2> 35℃/node1.2.2>
        < node1.2.3>医生和LT; /node1.2.3>
      < /node1.2>
    < /节点1>
< /nodeA.2>
< nodeA.3>汽车1 LT; /nodeA.3>
< / nodeA上>


解决方案

使用pt.get_child_optional(...),以prevent异常。 pt.find(...)返回其失败比较真实的pt.not_found()的迭代器。

编辑:如何使用boost ::可选< --->

 的boost ::可选< iptree&安培; > CHL = pt.get_child_optional(字段名);    如果(CHL){
      为(自动答:* CHL)
        的std :: CERR<< :&所述;&下; a.first<< :&所述;&下;的std :: ENDL;
    }

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>

解决方案

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; 
    } 

这篇关于查找XML与升压关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆