【问题标题】:Iterate throw specific xml attribute by Codesynthesis通过 Codesynthesis 遍历特定的 xml 属性
【发布时间】:2013-02-28 02:25:08
【问题描述】:

我有一个 xsd 文件,我想在属于它的 xml 上迭代抛出一个特殊属性 (Here is my xsd)。通过如下代码合成创建我的类后:

xsdcxx cxx-tree --root-element percolator_output --generate-polymorphic --namespace-map http://per-colator.com/percolator_out/14=xsd pout.xsd

我的主要内容如下:

int main (int argc, char* argv[])
{
  try
  {
   auto_ptr<percolator_output> h (percolator_output_ (argv[1]));
   //-----percolator_output::peptides_optional& pep (h->peptides ());
   for (peptides::peptide_const_iterator i (h->peptides ().begin ()); i != h->peptides ().end (); ++i)
   {
     cerr << *i << endl;
    }
  }
  catch (const xml_schema::exception& e)
  {
   cerr << e << endl;
   return 1;
  }
}

我想在我的 xml 文件中迭代 throw 属性“peptides”,但 h-&gt;peptides () 的输出是 percolator_output::peptides_optional,它不能迭代。

【问题讨论】:

    标签: c++ xml codesynthesis


    【解决方案1】:

    可选元素的存在首先需要通过函数present()来确认。如果元素存在,函数get() 可用于返回对该元素的引用。我尽可能少地修改了您的代码以使其能够编译。

    #include <iostream>
    #include <pout.hxx>
    
    using namespace std;
    using namespace xsd;
    
    int main (int argc, char* argv[])
    {
      try
      {
        auto_ptr<percolator_output> h (percolator_output_ (argv[1]));
        if (h->peptides().present())
        {
          for (peptides::peptide_const_iterator i (h->peptides ().get().peptide().begin ()); i != h->peptides ().get().peptide().end (); ++i)
          {
            cerr << *i << endl;
          }
        }
      }
      catch (const xml_schema::exception& e)
      {
       cerr << e << endl;
       return 1;
      }
    }
    

    此外,xsdcxx 缺少命令行参数 --generate-ostream

    $ xsdcxx cxx-tree --root-element percolator_output --generate-polymorphic --generate-ostream --namespace-map http://per-colator.com/percolator_out/14=xsd pout.xsd
    $ g++ -I. main.cc pout.cxx -lxerces-c
    $ cat /etc/issue
    Ubuntu 12.10 \n \l
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多