使用xerces for Android进行XML Schema验证

时间:2013-08-29 14:48:21

标签: android xml validation xsd xerces

我正在尝试针对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)

2 个答案:

答案 0 :(得分:2)

  

/device_description_schema.xsd:open failed:ENOENT(没有这样的文件或目录)

请检查文件的路径。

如果您的文件存储在此处,请使用Environment.getExternalStorageDirectory()获取SD卡的路径

希望有所帮助

编辑:使用getResources().openRawResource(R.raw.filename)获取/ res / raw

下文件的InputStream

答案 1 :(得分:0)

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

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

// FOLDER into assets if you need