2010-06-28 20 views
31

隨着Android SDK,在一個普通的空活動以下代碼失敗:SchemaFactory在平臺級別8不支持W3C XML Schema?

@Override 
protected void onStart() { 
    super.onStart(); 

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
} 

2.2模擬器logcat的示出了該異常:

06-28 05:38:06.107: WARN/dalvikvm(495): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
06-28 05:38:06.128: ERROR/AndroidRuntime(495): FATAL EXCEPTION: main 
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.HelloWorldActivity}: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
     at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:123) 
     at android.app.ActivityThread.main(ActivityThread.java:4627) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:521) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
     at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema 
     at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:194) 
     at com.example.HelloWorldActivity.onStart(HelloWorldActivity.java:26) 
     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
     at android.app.Activity.performStart(Activity.java:3781) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) 
     ... 11 more 

Javadoc of SchemaFactory提到「平臺默認的SchemaFactory位於一個特定於實現的方式,必須有一個用於W3C XML Schema的平臺默認SchemaFactory。「

+0

我有與平臺級別9和10相同的問題。正如[本答案](http://stackoverflow.com/questions/801632/android-schema/802150#802150)所述,似乎目前沒有Android中的XML Schema支持。 – 2011-09-22 11:27:01

+0

[看起來也類似](http://stackoverflow.com/questions/2694259/android-adt-eclipse-plugin-parsesdkcontent-failed)但答案沒有幫助 – 2011-11-27 02:18:03

回答

1

你可能有jarjar一些運氣重新包裝的Xerces,然後通過

"org.apache.xerces.jaxp.validation.XMLSchemaFactory" 

SchemaFactory.newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader) 

如果你使用的API> = 9或直接實例

org.apache.xerces.jaxp.validation.XMLSchemaFactory 

如果您使用的是API 8,則可能根本無法使用比此更早的API。

2

我有同樣的問題,並閱讀了很多帖子,然後我得到了一個適合我的答案。常數的參考不適用於Dalvik。我發現我必須修改我的代碼才能使用Xerces-for-Android項目,然後才能獲得我之後的xml驗證。這很可能是你正在做的變量引用。以下是設置的詳細信息和一些示例代碼,演示瞭如何使驗證在android上運行。

以下爲我工作:

  1. 創建一個驗證工具。
  2. 在android操作系統上同時獲取xml和xsd文件,並使用驗證實用程序來對付它。
  3. 使用Xerces-For-Android進行驗證。

的Android不支持一些軟件包,我們可以使用,我創建了一個基於我的XML驗證工具:http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

我最初的沙盒測試是用java相當順利,然後我試圖將它移植過來的Dalvik和發現我的代碼不起作用。有些事情與Dalvik並不相同,所以我做了一些修改。

我發現的xerces的Android的引用,所以我修改的沙箱試驗(下面不與機器人工作,在此之後的實例中確實):

import java.io.File; 

import javax.xml.XMLConstants; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Source; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 

import org.w3c.dom.Document; 

/** 
* A Utility to help with xml communication validation. 
*/ 
public class XmlUtil { 

    /** 
    * Validation method. 
    * Base code/example from: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html 
    * 
    * @param xmlFilePath The xml file we are trying to validate. 
    * @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid. 
    * @return True if valid, false if not valid or bad parse. 
    */ 
    public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) { 

     // parse an XML document into a DOM tree 
     DocumentBuilder parser = null; 
     Document document; 

     // Try the validation, we assume that if there are any issues with the validation 
     // process that the input is invalid. 
     try { 
      // validate the DOM tree 
      parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
      document = parser.parse(new File(xmlFilePath)); 

      // create a SchemaFactory capable of understanding WXS schemas 
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

      // load a WXS schema, represented by a Schema instance 
      Source schemaFile = new StreamSource(new File(xmlSchemaFilePath)); 
      Schema schema = factory.newSchema(schemaFile); 

      // create a Validator instance, which can be used to validate an instance document 
      Validator validator = schema.newValidator(); 
      validator.validate(new DOMSource(document)); 
     } catch (Exception e) { 
      // Catches: SAXException, ParserConfigurationException, and IOException. 
      return false; 
     }  

     return true; 
    } 
} 

上面的代碼有修改一些與android的xerces(http://gc.codehum.com/p/xerces-for-android/)一起使用。你需要SVN拿到項目,下面是一些嬰兒牀注意事項:

download xerces-for-android 
    download silk svn (for windows users) from http://www.sliksvn.com/en/download 
     install silk svn (I did complete install) 
     Once the install is complete, you should have svn in your system path. 
     Test by typing "svn" from the command line. 
     I went to my desktop then downloaded the xerces project by: 
      svn checkout http://xerces-for-android.googlecode.com/svn/trunk/ xerces-for-android-read-only 
     You should then have a new folder on your desktop called xerces-for-android-read-only 

通過上述罐子(最後我會讓它變成一個罐子,只是複製它直接進入我的源進行快速檢測。如果你想這樣做,你可以用Ant迅速使罐子(http://ant.apache.org/manual/using.html)),我是能夠得到以下爲我的XML驗證工作:

import java.io.File; 
import java.io.IOException; 

import mf.javax.xml.transform.Source; 
import mf.javax.xml.transform.stream.StreamSource; 
import mf.javax.xml.validation.Schema; 
import mf.javax.xml.validation.SchemaFactory; 
import mf.javax.xml.validation.Validator; 
import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory; 

import org.xml.sax.SAXException; 

/** 
* A Utility to help with xml communication validation. 
*/public class XmlUtil { 

    /** 
    * Validation method. 
    * 
    * @param xmlFilePath The xml file we are trying to validate. 
    * @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid. 
    * @return True if valid, false if not valid or bad parse or exception/error during parse. 
    */ 
    public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) { 

     // Try the validation, we assume that if there are any issues with the validation 
     // process that the input is invalid. 
     try { 
      SchemaFactory factory = new XMLSchemaFactory(); 
      Source schemaFile = new StreamSource(new File(xmlSchemaFilePath)); 
      Source xmlSource = new StreamSource(new File(xmlFilePath)); 
      Schema schema = factory.newSchema(schemaFile); 
      Validator validator = schema.newValidator(); 
      validator.validate(xmlSource); 
     } catch (SAXException e) { 
      return false; 
     } catch (IOException e) { 
      return false; 
     } catch (Exception e) { 
      // Catches everything beyond: SAXException, and IOException. 
      e.printStackTrace(); 
      return false; 
     } catch (Error e) { 
      // Needed this for debugging when I was having issues with my 1st set of code. 
      e.printStackTrace(); 
      return false; 
     } 

     return true; 
    } 
} 

一些旁註:

要創建這些文件,我做了一個簡單的文件工具寫字符串的文件:

public static void createFileFromString(String fileText, String fileName) { 
    try { 
     File file = new File(fileName); 
     BufferedWriter output = new BufferedWriter(new FileWriter(file)); 
     output.write(fileText); 
     output.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我還需要寫信給我不得不進入一個區域,所以我利用了:

String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir; 

有點ha,,它的工作原理。我確信有這樣一種更簡潔的方式,但我想我會分享我的成功,因爲我沒有找到任何好的例子。

+0

偉大的寫作,我欣賞細節的水平這個 – GMLewisII 2016-12-21 15:17:10