我有同樣的問題,並閱讀了很多帖子,然後我得到了一個適合我的答案。常數的參考不適用於Dalvik。我發現我必須修改我的代碼才能使用Xerces-for-Android項目,然後才能獲得我之後的xml驗證。這很可能是你正在做的變量引用。以下是設置的詳細信息和一些示例代碼,演示瞭如何使驗證在android上運行。
以下爲我工作:
- 創建一個驗證工具。
- 在android操作系統上同時獲取xml和xsd文件,並使用驗證實用程序來對付它。
- 使用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,,它的工作原理。我確信有這樣一種更簡潔的方式,但我想我會分享我的成功,因爲我沒有找到任何好的例子。
我有與平臺級別9和10相同的問題。正如[本答案](http://stackoverflow.com/questions/801632/android-schema/802150#802150)所述,似乎目前沒有Android中的XML Schema支持。 – 2011-09-22 11:27:01
[看起來也類似](http://stackoverflow.com/questions/2694259/android-adt-eclipse-plugin-parsesdkcontent-failed)但答案沒有幫助 – 2011-11-27 02:18:03