使用 Android 应用程序验证 xml 架构
所以目前我正在尝试设计一个自定义 xml 模式,并且 android 应用程序应该使用该模式。
有没有办法使用 android 验证 xml 架构?
So currently I am trying to design a custom xml schema and the android application is supposed to use that schema.
Is there a way to validate a xml schema using android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,不可能使用 W3C XML Schema(请参阅该帖子 还有更多类似的事情)。
如果您打算验证自定义 xml 格式并且此格式是否存在并且仅在您的应用程序中使用,我建议您通过使用 xmlpullparser 进行解析来以编程方式进行验证。如果互操作是您的目的,请进行服务器端验证(基于 W3C XML Schema 或 RelaxNG 等模式语言)和客户端简单完整性检查(Android 毕竟是一个嵌入式平台)...
我的另一个与 xml 模式相关的想法。
As far as I know, it is not possible to use W3C XML Schema (see that post and many more similar on SO).
I would suggest you to programmatically validate by parsing with xmlpullparser if you mean to validate your custom xml format and if this format exists and is used only within your application. If interop is your purpose, go for server-side validation (based on schema langage like W3C XML Schema or RelaxNG) and client-side simple integrity checking (android is an embedded platform after all)…
Another xml schema-related thought of mine.
我发现我无法让常规的 xerces 与 Android 一起使用,但是我确实找到了 Xerces-for-Android,我可以使用它。以下是设置的详细信息和一些示例代码。祝你好运:)
以下内容对我有用:
Android 确实支持一些我们可以使用的包,我根据以下内容创建了 xml 验证实用程序: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html
我的初始沙箱测试使用java非常顺利,然后我尝试将其移植到Dalvik,发现我的代码不起作用。有些东西与 Dalvik 的支持不同,所以我做了一些修改。
我找到了对 android 的 xerces 的引用,因此我修改了我的沙箱测试(以下不适用于 android,此后的示例适用):
上面的代码必须修改一些才能工作与 xerces for android (http://gc.codehum.com/p/xerces-for -android/)。你需要SVN来获取项目,以下是一些crib笔记:
用上面的jar(最终我会把它打成jar,直接复制到我的源中以便快速测试。如果你也想这样做,你可以使用 Ant 快速制作 jar (http://ant.apache.org/manual/using.html )),我能够得到以下内容用于我的 xml 验证:
一些旁注:
为了创建文件,我制作了一个简单的文件实用程序来将字符串写入文件:
我还需要写入我有权访问的区域,因此我利用了:
有点hackish,它有效。我确信有一种更简洁的方法可以做到这一点,但是我想我会分享我的成功,因为我没有找到任何好的例子。
I found I was not able to get the regular xerces to work with Android, however I did find Xerces-for-Android, which I got working. The following are details of the setup and some example code. Good luck :)
The following worked for me:
Android does support some packages which we can use, I created my xml validation utility based on: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html
My initial sandbox testing was pretty smooth with java, then I tried to port it over to Dalvik and found that my code did not work. Some things just aren't supported the same with Dalvik, so I made some modifications.
I found a reference to xerces for android, so I modified my sandbox test of (the following doesn't work with android, the example after this does):
The above code had to be modified some to work with xerces for android (http://gc.codehum.com/p/xerces-for-android/). You need SVN to get the project, the following are some crib notes:
With the above jar (Eventually I'll make it into a jar, just copied it directly into my source for quick testing. If you wish to do the same, you can making the jar quickly with Ant (http://ant.apache.org/manual/using.html)), I was able to get the following to work for my xml validation:
Some Side Notes:
For creating the files, I made a simple file utility to write string to files:
I also needed to write to an area that I had access to, so I made use of:
A little hackish, it works. I'm sure there is a more succinct way of doing this, however I figured I'd share my success, as there weren't any good examples that I found.