通过Codesynthesis迭代特定的xml属性

时间:2012-12-18 14:56:11

标签: c++ xml codesynthesis

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

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文件中抛出属性“peptides”但是h->peptides ()的输出是percolator_output::peptides_optional并且它不是迭代器。

1 个答案:

答案 0 :(得分: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;
  }
}

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

$ 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