简体   繁体   English

如何指定要使用的 XML 解析器实现?

[英]How can I specify which XML parser implementation to use?

I am currently trying to write an app (a game) that will, I hope, run on both desktop and Android.我目前正在尝试编写一个应用程序(一个游戏),我希望它可以在桌面和 Android 上运行。 The data format I have chosen to use is XML because it's more flexible and powerful than JSON, it performs better than a database and I am familiar with it and like it.我选择使用的数据格式是 XML,因为它比 JSON 更灵活、更强大,它的性能比数据库更好,而且我熟悉并喜欢它。

However, I'm beginning to wonder if it's going to actually be possible to use XML.但是,我开始怀疑是否真的可以使用 XML。

In addition to plain old XML I'm also using XInclude in my data structures and XPath to locate the relevant nodes.除了普通的旧 XML 之外,我还在我的数据结构和 XPath 中使用 XInclude 来定位相关节点。 This seems to require that I also validate my XML during parse, which is fine because I had already written the XML Schema when developing the XML in the first place.这似乎要求我在解析过程中也验证我的 XML,这很好,因为我首先在开发 XML 时已经编写了 XML 模式。

I've managed to get everything working on the desktop, however, as soon as I tried to run it on Android it failed.我已经设法让一切都在桌面上运行,但是,一旦我尝试在 Android 上运行它,它就失败了。 It seems that the XML parser used by default on Android doesn't support XInclude ( source ). Android 上默认使用的 XML 解析器似乎不支持 XInclude ( source )。

I've added the following line to my build.gradle:我在 build.gradle 中添加了以下行:

compile "xerces:xercesImpl:2.12.0"

and it seems to have worked in that the SchemaFactory seems to be resolving to the Xerces one as evidenced by the debugging output:正如调试输出所证明的那样,似乎 SchemaFactory 似乎正在解析为 Xerces ,这似乎是有效的:

08-21 14:19:35.488 8254-8332 W/System.err: JAXP: Reading jar:file:/data/app/uk.co.redfruit.gdx.wobbegong-geDT8AfzN-vHBlDA-lE0_g==/base.apk!/META-INF/services/javax.xml.validation.SchemaFactory
    JAXP: instantiating org.apache.xerces.jaxp.validation.XMLSchemaFactory

But the DocumentBuilderFactory appears to be using the default Android one because it still throws the error:但是 DocumentBuilderFactory 似乎正在使用默认的 Android 一个,因为它仍然抛出错误:

java.lang.UnsupportedOperationException: This parser does not support specification "Unknown" version "0.0"

Which is what is always thrown when you call setXIncludeAware on the DocumentBuilderFactory (See above link).这是在 DocumentBuilderFactory 上调用 setXIncludeAware 时总是抛出的(见上面的链接)。

I had assumed that including it in my gradle build file would do the trick and it seems like it partly has, but not enough.我原以为将它包含在我的 gradle 构建文件中就可以解决问题,看起来它部分具有,但还不够。

I'm hoping that all crosss platform XML parsing problems will be solved by using the same parser implementation on all platforms, but it doesn't seem as straight forward as that...我希望通过在所有平台上使用相同的解析器实现来解决所有跨平台 XML 解析问题,但它似乎并不那么简单......

Take a look at the javadocs for DocumentBuilderFactory.newInstance()查看DocumentBuilderFactory.newInstance()的 javadocs

Use the javax.xml.parsers.DocumentBuilderFactory system property.使用 javax.xml.parsers.DocumentBuilderFactory 系统属性。

I suggest you set the javax.xml.parsers.DocumentBuilderFactory system property to the classname of the xerces DocumentBuilderFactory implementation我建议您将javax.xml.parsers.DocumentBuilderFactory系统属性设置为 xerces DocumentBuilderFactory 实现的类名

I'm guessing it's this is the class you want我猜就是你想要的课程

eg:例如:

System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
doFunkyXmlStuff();

To specify the parser to be used as Xerces-2, I would suggest to pass the parser classname when instantiating the DocumentBuilderFactory as follows:要指定用作 Xerces-2 的解析器,我建议在实例化 DocumentBuilderFactory 时传递解析器类名,如下所示:

DocumentBuilderFactory docFactory = DocumentBuilderFactory
        .newInstance("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl", null);
docFactory.setXIncludeAware(true);

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

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