尽管我是 xml 解析领域的新手,但我能够通过 xsd
创建有效的 c++
并成功编译和链接,但是编译器优化了( ?)离开实例化。所以,从第一步开始,我尝试 hello world xml example at CodeSynthesis .但这失败了:
[wally@lenovotower xml]$ make hello
xsdcxx cxx-tree hello.xsd
g++ -c -o helloschema.o hello.cxx
g++ -g -o hello -lxerces-c helloschema.o hello.c++
[wally@lenovotower xml]$ ./hello
hello.xml:2:8 error: no declaration found for element 'hello'
hello.xml:4:13 error: no declaration found for element 'greeting'
hello.xml:6:9 error: no declaration found for element 'name'
hello.xml:7:9 error: no declaration found for element 'name'
hello.xml:8:9 error: no declaration found for element 'name'
你好.c++:
#include <iostream>
#include <stdio.h>
#include "hello.hxx"
using namespace std;
int main (void)
{
try {
auto_ptr<hello_t> h (hello ("hello.xml"));
for (hello_t::name_const_iterator i (h->name ().begin());
i != h->name().end();
++i)
cout << h->greeting () << ", " << *i << "!" << endl;
}
catch (const xml_schema::exception& e)
{
cerr << e << endl;
return 1;
}
return 0;
}
你好.xml:
<?xml version="1.0"?>
<hello>
<greeting>Hello</greeting>
<name>sun</name>
<name>moon</name>
<name>world</name>
</hello>
你好.xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="hello_t">
<xs:sequence>
<xs:element name="greeting" type="xs:string"/>
<xs:element name="name" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="hello" type="hello_t"/>
</xs:schema>
我认为这正是它所说的,但这些命令并不完全按照记录的那样工作。我发现 xsdcxx
似乎做了正确的事情(与生成 C# 或 vb.net 输出的 xsd
不同)。
[wally@lenovotower xml]$ xsdcxx --version
CodeSynthesis XSD XML Schema to C++ compiler 3.3.0
Copyright (C) 2005-2010 Code Synthesis Tools CC
此外,我没有包含 -I
(dir) 并且它可以愉快地编译。它会不会以某种方式使用了错误的包含文件?
我做错了什么?也许 xsd
不是正确的工具?
最佳答案
有一些选择。模式位置可以在文件 hello.xml 中提供:
<?xml version="1.0"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="hello.xsd">
<greeting>Hello</greeting>
<name>sun</name>
<name>moon</name>
<name>world</name>
</hello>
另一种选择是在文件 hello.c++
中提供架构位置如果我们假设模式文件位于文件路径 /some/file/path/hello.xsd
我们应该而不是写
auto_ptr<hello_t> h (hello ("hello.xml"));
写
xml_schema::properties properties;
properties.no_namespace_schema_location("file:///some/file/path/hello.xsd");
auto_ptr<hello_t> h (hello ("hello.xml", 0, properties));
您可以在 Codesynthesis FAQ 中阅读更多相关信息:
关于c++ - 示例 XSD 失败并返回 "error: no declaration found for element X",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6393689/