本文介绍了Xerces-C使用硬编码的xsd验证xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个接受xml文件并对其进行解析的库.为了防止用户将inalid xml馈入我的应用程序,我正在使用xerces通过xsd验证xml文件.

I'm writing a library which takes xml files and parses them. To prevent users from feeding inalid xmls into my application i'm using xerces to validate the xml files via an xsd.

但是,我只设法针对xsd文件进行验证.从理论上讲,用户可以打开该文件并弄乱它.这就是为什么我希望在我的库中对我的xsd进行硬编码.

However, i only manages to validate against xsd-files. Theoretically an user could just open this file and mess around with it. That's why i would like my xsd to be hardcoded in my library.

不幸的是,我还没有找到使用XercesC ++做到这一点的方法.

Unfortunately i haven't found a way to do this with XercesC++, yet.

这就是现在的工作方式...

That's how it is working right now...

bool XmlParser::validateXml(std::string a_XsdFilename)
{
    xercesc::XercesDOMParser  domParser;
    if (domParser.loadGrammar(a_XsdFilename.c_str(), xercesc::Grammar::SchemaGrammarType) == NULL)
    {
        throw Exceptions::Parser::XmlSchemaNotReadableException();
    }

    XercesParserErrorHandler parserErrorHandler;

    domParser.setErrorHandler(&parserErrorHandler);
    domParser.setValidationScheme(xercesc::XercesDOMParser::Val_Always);
    domParser.setDoNamespaces(true);
    domParser.setDoSchema(true);
    domParser.setValidationSchemaFullChecking(true);

    domParser.parse(m_Filename.c_str());

    return (domParser.getErrorCount() == 0);

}

std::string m_Filename是保存我验证的xml路径的成员变量.

std::string m_Filename is a member variable holding the path of the xml i validate.

std::string a_XsdFilename是我验证所依据的xsd的路径.

std::string a_XsdFilename is the path to the xsd i validate against.

XercesParserErrorHandlerxercesc::ErrorHandler继承并进行错误处理.

XercesParserErrorHandler inherits from xercesc::ErrorHandler and does error handling.

如何用std::string a_XsdText之类的东西替换std::string a_XsdFilename?其中std::string a_XsdText包含架构定义本身,而不是包含架构定义的文件的路径.

How can i replace std::string a_XsdFilename with something like std::string a_XsdText?Where std::string a_XsdText contains the schema definition itself instead of a path to a file containing the schema definition.

推荐答案

我将介绍三种在程序中对XSD进行硬编码的方法:

I'll describe three ways of how to hardcode your XSD in your program:

  • 通过从文件路径加载XSD(这是您的示例程序现在所做的事情)
  • 通过从字符串加载XSD(这就是您所要求的)
  • 通过预编译的二进制文件加载XSD

鲍里斯·科尔帕科夫(Boris Kolpackov)在中建议博客文章,即应用程序应自行提供XSD架构文件,而不是通过在 xsi:schemaLocation xsi:noNamespaceSchemaLocation 属性中查找架构文件. XML文件.

Boris Kolpackov suggests in a blog post that applications should provide the XSD schema files by themselves rather than looking up the schema files through the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attributes found in the XML file.

博客文章中有指向 load-grammar-dom ,这是一个使用 xercesc :: DOMLSParser :: loadGrammar 函数:

In the blog post there is a link to load-grammar-dom , an example program (put in the public domain) that makes use of the xercesc::DOMLSParser::loadGrammar function:

user@linux:~$ load-grammar-dom
usage: load-grammar-dom [test.xsd ... ] [test.xml ...]
user@linux:~$

从字符串加载XSD

如果您想将XSD文件的内容作为字符串传递,则需要使用的另一个重载. xercesc :: DOMLSParser :: loadGrammar 您通过的地方

Loading the XSD from a string

If you would like to pass the XSD file contents as a string, you would need to use another overload ofxercesc::DOMLSParser::loadGrammarwhere you pass

const DOMLSInput *source

代替

const char *const systemId

可以在 xercesc :: MemBufInputSource的帮助下创建DOMLSInput. xercesc :: Wrapper4InputSource 像这样

xercesc::Wrapper4InputSource source(
    new xercesc::MemBufInputSource(
       (const XMLByte *) (a_XsdText.c_str()),
    a_XsdText.size(),
    "A name");

(改编自 https://stackoverflow.com/a/15829424/757777 ,但未经测试)

(Adapted somewhat fromhttps://stackoverflow.com/a/15829424/757777 but untested)

包含在软件 CodeSynthesis XSD 中. codeynthesis.com/cgit/xsd/xsd/tree/examples/cxx/tree/embedded"rel =" nofollow noreferrer> 嵌入 示例(已放置在公共领域中)进行了演示使用方法

Included in the software CodeSynthesis XSD the embedded example (that is put in the public domain) demonstrates how to use

xercesc :: BinInputStream xercesc :: XMLGrammarPool :: deserializeGrammars

加载预编译的XSD架构.

to load a precompiled XSD schema.

另请参见自述文件

该示例包含程序xsdbin,该程序将XSD模式文件编译为二进制文件.

The example contains the program xsdbin that compiles XSD schema files into a binary file.

user@linux:~$ xsdbin --help
Usage: xsdbin [options] <files>
Options:
  --help                 Print usage information and exit.
  --verbose              Print progress information.
  --output-dir <dir>     Write generated files to <dir>.
  --hxx-suffix <sfx>     Header file suffix instead of '-schema.hxx'.
  --cxx-suffix <sfx>     Source file suffix instead of '-schema.cxx'.
  --array-name <name>    Binary data array name.
  --disable-multi-import Disable multiple import support.
user@linux:~$

makefile 中XSD模式文件由xsdbin预编译,结果最终存储在示例可执行文件内.

In the makefile the XSD schema file is precompiled by xsdbin and the result ends up inside the example executable.

这篇关于Xerces-C使用硬编码的xsd验证xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 20:02
查看更多