简体   繁体   English

使用XMLSchema在配置解析代码中进行XML验证:最佳实践? (Java)

[英]XML Validation in configuration parsing code with XMLSchema: Best Practices? (Java)

I have to parse a XML configuration file. 我必须解析一个XML配置文件。 I decided to do the basic validation with XMLSchema to avoid massive amounts of boilerplate code for validation. 我决定使用XMLSchema进行基本验证,以避免进行大量验证的样板代码。 Since the parsing class should work for itself i was wondering: "How can i validate the incoming XML file with XMLSchema without having the XMLSchema stored on disc where it can be modified by third parties?" 由于解析类应该自己工作,所以我想知道:“如何在不将XMLSchema存储在磁盘上可以由第三方修改的情况下,使用XMLSchema验证传入的XML文件?” I know i can load the XMLSchema from an arbitrary InputStream, but the Schema itself must be stored somewhere. 我知道我可以从任意InputStream加载XMLSchema,但是Schema本身必须存储在某个地方。 (And putting the Schema in a huge String object sounds like a very very bad idea) Has someone already did this? (并且将Schema放入巨大的String对象中听起来像是一个非常非常糟糕的主意)有人已经这样做了吗? What are the best solution for such cases? 此类情况的最佳解决方案是什么?

Have a nice start of the week! 祝您一周愉快!

I would place it in your deployment alongside the appropriate classes. 我会将其与适当的类一起放在您的部署中。 Load it using 使用加载

ClassLoader.getResourceAsStream() ClassLoader.getResourceAsStream()

If you're really worried about someone modifying this schema, why not deploy in a .jar file and then sign the .jar file ? 如果您真的担心有人会修改此架构,为什么不将其部署在.jar文件中,然后对.jar文件签名 That way you can ensure nobody will tamper with it, and you don't have to rely on third-party infrastructures, storing it remotely etc. 这样,您可以确保没有人会篡改它,并且您不必依赖第三方基础结构,远程存储它等。

将其放在公共网站上。

Could stick it in a DB. 可以将其粘贴到数据库中。 I know Oracle has some sort of support for XMLSchemas. 我知道Oracle对XMLSchemas有某种支持。

Karl 卡尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM