2013-08-29 76 views
1

我試圖根據XML模式文件驗證XML文件,並且已下載了https://code.google.com/p/xerces-for-android/庫以使XML模式驗證正常工作,並且我已將該項目創建爲庫。我無法使其正常工作,並且出現'FileNotFoundException'錯誤,如果有人可以看到我做錯了什麼,或者我需要做些什麼才能使其工作,那麼請你能幫助我。使用xerces for Android進行XML模式驗證

下面是代碼:

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

/device_description_schema.xsd:打開失敗:ENOENT(沒有這樣的文件或目錄)

請檢查文件的路徑。

使用Environment.getExternalStorageDirectory()獲得SD卡的完整路徑,如果你的文件存儲在這裏

希望它可以幫助

編輯:使用getResources().openRawResource(R.raw.filename)獲得下/ RES文件的一個InputStream /生

+0

我只有存儲的文件n我的項目文件夾res/raw /下。我覺得我在這裏稍微做錯了一件事,它正在設備上尋找文件,顯然它不在那裏。我應該如何解決這個問題? –

+0

請檢查我的編輯 –

+0

我試過了,它不會檢測到原始文件夾中的文件。我應該嘗試再次創建文件夾還是有另一種方式? –

0

對於Android,您必須將您的文件放入資產目錄並使用Android環境打開文件:

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

// FOLDER into assets if you need