Вопрос

I have an xsd file and I want to iterate throw an special attribute on the xml belongs to it (Here is my xsd). After creating my classes by codesynthesis like below:

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

I've writing my main like :

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

I want to iterate throw the attribute "peptides" on my xml file but the output of h->peptides () is percolator_output::peptides_optional and it's not iterator-able.

Это было полезно?

Решение

The presence of the optional element first need to be confirmed by using the function present(). If element is present, the function get() can be used to return a reference to the element. I modified your code as little as possible to make it compile.

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

And also, the command line argument --generate-ostream was missing to 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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top