我正在尝试使用Android 15中的exificient-grammars库创建语法(c是上下文)。
Grammars g = GrammarFactory.newInstance().createGrammars(c.getAssets().open("svg.xsd"));从svg.xsd中导入两个模式: xlink.xsd和namespace.xsd。这两个文件伴随着svg.xsd (如您所见,它们位于svg.xsd 这里的根目录中)。但是,我没有创建语法,而是得到了这个例外:
com.siemens.ct.exi.exceptions.EXIException: Problem occured while building XML Schema Model (XSModel)!
. [xs-warning] schema_reference.4: Failed to read schema document 'xlink.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
. [xs-warning] schema_reference.4: Failed to read schema document 'namespace.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.使用导入的svg.xsd的两行如下:
<xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="namespace.xsd"/>到目前为止我尝试过的:
SchemaInformedGrammars.class,但我不明白systemId是什么。com.siemens.ct.exi.grammars.XSDGrammarsBuilder创建语法:XSDGrammarsBuilder xsd = XSDGrammarsBuilder.newInstance();
xsd.loadGrammars(c.getAssets().open("namespace.xsd"));
xsd.loadGrammars(c.getAssets().open("xlink.xsd"));
xsd.loadGrammars(c.getAssets().open("svg.xsd"));
SchemaInformedGrammars sig = xsd.toGrammars();
exiFactory.setGrammars(sig);却得到了同样的错误..。
我的问题是:问题似乎是解析器无法找到另外两个文件。有没有一种方法可以以某种方式包含这些文件,以便解析器能够找到它们?
发布于 2019-10-07 13:42:57
来自高级开发团队的danielpeintner把我推到了正确的方向(发布这里)。
丹尼尔建议我不要使用createGrammar(InputStream),而是使用createGrammar(String, XMLEntityResolver),并提供自己的XMLEntityResolver实现。我的实施是:
public class XSDResolver implements XMLEntityResolver {
Context context;
public XSDResolver(Context context){
this.context = context;
}
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
String literalSystemId = resourceIdentifier.getLiteralSystemId();
if("xlink.xsd".equals(literalSystemId)){
InputStream is = context.getAssets().open("xlink.xsd");
return new XMLInputSource(null, null, null, is, null);
} else if("namespace.xsd".equals(literalSystemId)){
InputStream is = context.getAssets().open("namespace.xsd");
return new XMLInputSource(null, null, null, is, null);
} else if("svg.xsd".equals(literalSystemId)){
InputStream is = context.getAssets().open("svg.xsd");
return new XMLInputSource(null, null, null, is, null);
}
return null;
}
}像这样调用createGrammar(String, XMLEntityResolver):
exiFactory.setGrammars(GrammarFactory.newInstance().createGrammars("svg.xsd", new XSDResolver(c)));https://stackoverflow.com/questions/58255691
复制相似问题