首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析/验证xml,不包括使用codesynthesis的xsd信息。

解析/验证xml,不包括使用codesynthesis的xsd信息。
EN

Stack Overflow用户
提问于 2019-07-22 08:26:55
回答 1查看 366关注 0票数 1

我遇到了一个类似于this question的问题。基本上,如果我的xml不包含有关xsd的信息,我就会得到错误。下面给出了xml、xsd和一个给出错误的示例程序。

hello.xml

代码语言:javascript
复制
<?xml version="1.0"?>
<hello>

  <greeting>Hello</greeting>

  <name>sun</name>
  <name>moon</name>
  <name>world</name>

</hello>

如果我在开始时用下面的代码替换了“hello”标签,那么程序就会运行得很好。

代码语言:javascript
复制
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="hello.xsd">

hello.xsd

代码语言:javascript
复制
<?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>

main.cpp

代码语言:javascript
复制
#include <iostream>
#include "hello.hxx"

using namespace std;

int
main (int argc, char* argv[])
{
  try
  {
    unique_ptr<hello_t> h (hello (argv[1]));

    for (hello_t::name_const_iterator i (h->name ().begin ());
         i != h->name ().end ();
         ++i)
    {
      cerr << h->greeting () << ", " << *i << "!" << endl;
    }
  }
  catch (const xml_schema::exception& e)
  {
    cerr << "exception caught("<<e<<"): "<<e.what() << endl;
    return 1;
  }
}

Error

代码语言:javascript
复制
exception caught(/home/vishal/testing/hello.xml:2:8 error: no declaration found for element 'hello'
/home/vishal/testing/hello.xml:4:13 error: no declaration found for element 'greeting'
/home/vishal/testing/hello.xml:6:9 error: no declaration found for element 'name'
/home/vishal/testing/hello.xml:7:9 error: no declaration found for element 'name'
/home/vishal/testing/hello.xml:8:9 error: no declaration found for element 'name'): instance document parsing failed

我想知道是否有一种方法可以避免这个问题,而不需要在xml中指定xsd信息。如果xml不符合xsd,我还希望解析器抛出一个错误(就像现在一样)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-01 10:09:18

正如Erik Sjolund在评论中所建议的那样,我补充了以下内容:

代码语言:javascript
复制
xml_schema::properties props;
props.no_namespace_schema_location ("hello.xsd");
unique_ptr<hello_t> h (hello (argv[1],0,props));

现在没有必要在xml中提到xsd的路径。

谢谢埃里克!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57141880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档