gpt4 book ai didi

c++ - 通过 Codesynthesis 遍历特定的 xml 属性

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:25 27 4
gpt4 key购买 nike

我有一个 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 文件上迭代抛出属性“肽”,但是 h->peptides () 的输出是 percolator_output::peptides_optional 并且它不是迭代器-能够。

最佳答案

首先需要使用函数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

关于c++ - 通过 Codesynthesis 遍历特定的 xml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13935601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com