Вопрос

I am trying to validate an XML file against an XML Schema file and I have downloaded the https://code.google.com/p/xerces-for-android/ library to get XML Schema validation to work and I have created the project as a library. I am having trouble getting it to work properly for Android and I get the 'FileNotFoundException' error and if anyone can see what I am doing wrong or what I need to do to get it to work, then please can you help me.

Here is the code:

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());
    }
}

And here is the logcat errors:

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)
Это было полезно?

Решение

/device_description_schema.xsd: open failed: ENOENT (No such file or directory)

Please check the path of the file.

Use Environment.getExternalStorageDirectory() to get the path of the sdcard if your file is stored here

Hope it helps

EDIT : Use getResources().openRawResource(R.raw.filename) to get an InputStream of a file under /res/raw

Другие советы

For Android, you must put your files into assets directory and open the file with the Android context:

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

// FOLDER into assets if you need
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top