使用xsd cxx-tree解析器的XSD到C ++类。从文件中打开xml的方法。

时间:2014-02-26 11:04:30

标签: c++ xsd xerces local-files codesynthesis

我使用xsd 3.3.0编译器以便将xsd(xml最好的朋友)文件解析为C ++类。 (见最后一个网站链接)

命令名称是

  

xsd cxx-tree(options)file.xsd

(+ info http://www.codesynthesis.com/projects/xsd/documentation/cxx/tree/guide/

我见过codesynthesis提供的一些示例,它们解析hello.xsd文档并非常容易地创建.hxx和.cxx文件。 .hxx有一个方法来打开一个xml文档创建一个对象,你可以在其中找到xml的不同部分,检查它等等... .hxx有这样的代码:

// Parse a URI or a local file.
//

::std::auto_ptr< ::hello_t >
hello (const ::std::string& uri,
       ::xml_schema::flags f = 0,
       const ::xml_schema::properties& p = ::xml_schema::properties ());

它收到一个文件名为

的字符串
  

的字符串&安培; uri =“hello.xsd”

并创建在main.cxx中使用的对象。

所以,我正在尝试对我的xsd文件做同样的事情。我使用xsd cxx-tree编译器,但它没有创建“解析URI或本地文件”的方法。然后我无法在主程序的xml文件中创建对象。

我使用来自codesys编译器文档(http://www.codesynthesis.com/projects/xsd/documentation/xsd.xhtml)的不同选项解决了一些编译问题。有关于你想要编译什么,你想怎么做等等的不同选项......但我找不到任何选项来启用“解析URI或本地文件”的方法。“ / p>

提供更多信息,xml-xsd文档是CBML协议文档。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我一直都在使用这个产品。

以下是您尝试做的一个示例(我认为):

xsd cxx-tree hello.xsd

正如您所说,正在生成hello.hxxhello.cxx。我认为你不知道如何使用这些文件来加载XML文件(例如,加载“本地文件”)。

我喜欢明确告诉软件在哪里找到XSD架构。以下代码不会编译,但我已将其包含在内以供参考。

void load(const std::string &xml, const std::string &xsd) {
    xml_schema::properties properties;
    properties.no_namespace_schema_location(xsd);
    // or, if you have a specific namespace you want to load, use
    // props.schema_location("which namespace", xsd);
    try {
        std::auto_ptr< ::hello_t> xml_file = hello(xml, 0, props);
        // operate on the xml_file via "->" notation
        std::cout << xml_file->hello() << std::endl;

    } catch (const ::xml_schema::exception &e) {
        std::ostringstream os;
        os << e.what();
        // report the error as you see fit, use os.str() to get the string
    }

}

确保将*.cxx文件与您的*.cpp文件相关联。

如果这不是您想要的,请在评论中告诉我,我可以尝试为您提供更多帮助。

答案 1 :(得分:0)

我自己找到了解决方案!

我使用了不同的选项进行编译。我使用了选项“--root-element library”,它导致了“解析URI或本地文件”的方法。没有被创造。

我删除了这个选项,并添加了“--root-element-all”,为所有主体对象创建了解析方法!

现在我的程序正常运作!谢谢!