1

我正在尝试针对 XML Schema 文件验证 XML 文件,并且我已经下载了https://code.google.com/p/xerces-for-android/库以使 XML Schema 验证正常工作,并且我已经创建了项目作为图书馆。我无法让它在 Android 上正常工作,并且出现“FileNotFoundException”错误,如果有人能看到我做错了什么或者我需要做什么才能让它工作,那么请你帮我。

这是代码:

public void xmlValidator() throws IOException, SAXException
{
    Source schemaFile = new StreamSource(new File("device_description_schema.xsd"));
    Source xmlFile = new StreamSource(new File("device_description.xml"));
    SchemaFactory schemaFactory = new XMLSchemaFactory();
    Schema schema = schemaFactory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    try {
      validator.validate(xmlFile);
      System.out.println(xmlFile.getSystemId() + " is valid");
    } catch (SAXException e) {
      System.out.println(xmlFile.getSystemId() + " is NOT valid");
      System.out.println("Reason: " + e.getLocalizedMessage());
    }
}

这是 logcat 错误:

08-29 15:05:09.461: W/System.err(14294): java.io.FileNotFoundException: /device_description_schema.xsd: open failed: ENOENT (No such file or directory)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:197)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:133)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:395)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:305)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4098)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument1(XSDHandler.java:2452)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:2161)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:558)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:582)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:549)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:515)
08-29 15:05:09.461: W/System.err(14294):    at mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:237)
08-29 15:05:09.461: W/System.err(14294):    at mf.javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:611)
08-29 15:05:09.461: W/System.err(14294):    at com.example.mobileapplicationretry.generalclasses.ValidateXml.xmlValidator(ValidateXml.java:49)
08-29 15:05:09.461: W/System.err(14294):    at com.example.mobileapplicationretry.MainActivity.onCreate(MainActivity.java:52)
08-29 15:05:09.461: W/System.err(14294):    at android.app.Activity.performCreate(Activity.java:5133)
08-29 15:05:09.461: W/System.err(14294):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-29 15:05:09.461: W/System.err(14294):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-29 15:05:09.461: W/System.err(14294):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-29 15:05:09.461: W/System.err(14294):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-29 15:05:09.461: W/System.err(14294):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-29 15:05:09.461: W/System.err(14294):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 15:05:09.461: W/System.err(14294):    at android.os.Looper.loop(Looper.java:137)
08-29 15:05:09.461: W/System.err(14294):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-29 15:05:09.461: W/System.err(14294):    at java.lang.reflect.Method.invokeNative(Native Method)
08-29 15:05:09.461: W/System.err(14294):    at java.lang.reflect.Method.invoke(Method.java:525)
08-29 15:05:09.461: W/System.err(14294):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-29 15:05:09.461: W/System.err(14294):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-29 15:05:09.461: W/System.err(14294):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

/device_description_schema.xsd:打开失败:ENOENT(没有这样的文件或目录)

请检查文件的路径。

如果您的文件存储在此处,则用于Environment.getExternalStorageDirectory()获取 sdcard 的路径

希望能帮助到你

编辑:getResources().openRawResource(R.raw.filename)用于获取 /res/raw 下文件的 InputStream

于 2013-08-29T14:57:17.330 回答
0

对于 Android,您必须将文件放入 assets 目录并使用 Android 上下文打开文件:

Context myAndroidContext;
...
final Source xsdSource = new StreamSource(myContext.getAssets().open(
                FOLDER  + File.separator + xsdFileName));
...

// FOLDER into assets if you need
于 2014-08-20T10:07:17.457 回答