Java Android中的XML模式验证
我已经创建了一个XML,我想用模式进行验证,即, XSD文件,但android没有为 如果我没有错的话,也是一样。。。。。。。。。还有一个名为Java Android中的XML模式验证,java,android,Java,Android,我已经创建了一个XML,我想用模式进行验证,即, XSD文件,但android没有为 如果我没有错的话,也是一样。。。。。。。。。还有一个名为 jaxp1.3不允许我编译代码是因为 桌面和安卓的字节码不同?哪个有班级 模式工厂和验证器,负责验证。。。。。。是 还有其他选择。任何帮助都将不胜感激。。。。。 绝望地寻找答案 这是谷歌发布的一个已知问题 解决方案是使用ApacheXerces移植到Android。 有一个项目 您必须执行svn chekout并将项目导出到jar文件,以用作androi
jaxp1.3
不允许我编译代码是因为
桌面和安卓的字节码不同?哪个有班级
模式工厂和验证器,负责验证。。。。。。是
还有其他选择。任何帮助都将不胜感激。。。。。
绝望地寻找答案 这是谷歌发布的一个已知问题 解决方案是使用ApacheXerces移植到Android。 有一个项目 您必须执行svn chekout并将项目导出到jar文件,以用作android项目中的库 实例SchemaFactory的代码略有更改。 我举一个例子:
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;
SchemaFactory factory = new XMLSchemaFactory();
Schema esquema = factory.newSchema(".../file.xsd");
@iOSDev,我不得不使用Xerces for Android进行验证。以下是我为使其与我的程序一起工作所做的总结:
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
使用上面的jar(最终我会将其制作成一个jar,直接复制到我的源代码中进行快速测试。如果您希望这样做,可以使用Ant()快速制作jar),我能够获得以下用于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;
有点老套,它很管用。我相信有一种更简洁的方法可以做到这一点,但我想我会分享我的成功,因为我没有找到任何好的例子
String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;